I try to initialize and assign an instance of a custom class to a BindableProperty
in a ResourceDictionary
(styles.xaml).
My class:
namespace Foo {
public class Colors {
public Color Positive { get; set; }
public Color Negative { get; set; }
public Color Neutral { get; set; }
public Colors() { }
}
}
My class with BindableProperty:
namespace Foo {
public class Bar : ContentView {
public Colors Colors {
get => (Colors)GetValue(ColorsProperty);
set => SetValue(ColorsProperty, value);
}
public static BindableProperty ColorsProperty =
BindableProperty.Create(nameof(Colors), typeof(Colors),
typeof(Bar), null, BindingMode.OneWay);
styles.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:foo="clr-namespace:Foo;assembly=Foo">
<foo:Colors
x:Key="colors"
Positive="Green"
Negative="Red"
Neutral="Blue"/>
...
<Style TargetType="foo:Bar">
<Setter Property="Colors" Value="{StaticResource colors}"/>
</Style>
</ResourceDictionary>
A breakpoint in the constructor of class Foo.Colors
is hit, so an instance of this class is created during loading styles.xaml. However loading styles.xaml crashes with an unhandled NullReferenceException
inside Mono.Android. When i delete the assignment of the Color-Property everything works fine, so the assignment must be the problem.
Any suggestions?
Exception Details:
System.NullReferenceException: Object reference not set to an instance of an object.
at Android.Runtime.JNINativeWrapper._unhandled_exception (System.Exception e) [0x0000e] in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:12
at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPL_V (_JniMarshal_PPL_V callback, System.IntPtr jnienv, System.IntPtr klazz, System.IntPtr p0) [0x0001d] in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:111
at (wrapper native-to-managed) Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPL_V(intptr,intptr,intptr)
The Exception occures in App.xaml.g.cs, after global::Xamarin.Forms.Xaml.Extensions.LoadFromXaml(this, typeof(App));
2条答案
按热度按时间8nuwlpux1#
我找到了一个适合我的简单解决方案:
在styles.xaml中,我将
StaticResource
替换为DynamicResource
:感谢所有的建议!
1tu0hz3e2#
您似乎想要使用独立的资源字典。过去,当我遵循Microsoft的相关文件时,我在使用这些资源字典时遇到过问题。我总是必须将字典设定成在另一个组件中:
styles.xaml:
请注意,此文件确实需要一个简单的代码:
添加此文件最简单的方法是添加一个带有xaml的ContentView,然后将xaml和cs更改为从ResourceDictionary继承。
从此处,将文件添加到应用程序或页面的合并词典中:
这应该可以解决您的错误。示例如下。
Bar.cs类别:
xaml类:
运行结果: