当我尝试将全局样式应用于Xamarin表单中的自定义ContentView时,出现C#-NullReferenceException
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了当我尝试将全局样式应用于Xamarin表单中的自定义ContentView时,出现C#-NullReferenceException,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4230字,纯文字阅读大概需要7分钟。
内容图文
![当我尝试将全局样式应用于Xamarin表单中的自定义ContentView时,出现C#-NullReferenceException](/upload/InfoBanner/zyjiaocheng/685/75c7acffbdc64c4fb5533aabdc628a7d.jpg)
我为项目创建了控件层次结构:抽象BaseSettingsElement并继承了EntrySettingsElement,PickerSettingsElement,SwitchSettingsElement等.基类提供了更改标题,子标题的文本,颜色,字体的属性.属性声明示例:
public static readonly BindableProperty HeadTextColorProperty =
BindableProperty.Create("HeadTextColor", typeof(Color), typeof(BaseSettingsElement), Color.FromHex("#ffffff"),
propertyChanged: (bindable, oldValue, newValue) =>
{
(bindable as BaseSettingsElement).headText.TextColor = (Color)newValue;
});
//...
public Color HeadTextColor
{
get { return (Color)GetValue(HeadTextColorProperty); }
set { SetValue(HeadTextColorProperty, value); }
}
在xaml中创建此控件并将一些属性应用于它们时,没有问题:
<custom:EntrySettingsElement Grid.Row="16"
InputType="Number"
DividersVisibility="None"
IsEnabled="False"
HeadTextColor="Red"
Text="0" />
但是,当我尝试将app.xaml中的全局样式应用于某些控件时,在这里有一个NullRefferenceException:
public static readonly BindableProperty HeadTextColorProperty =
BindableProperty.Create("HeadTextColor", typeof(Color), typeof(BaseSettingsElement), Color.FromHex("#ffffff"),
propertyChanged: (bindable, oldValue, newValue) =>
{
(bindable as BaseSettingsElement).headText.TextColor = (Color)newValue;
//Here is a NullReferenceException: bindable object is not null but the field 'headText' is null...
});
以防万一基本控件的xaml:
<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="RemoteControlApp.CustomViews.BaseSettingsElement"
xmlns:custom="clr-namespace:RemoteControlApp.CustomViews;assembly=RemoteControlApp">
<ContentView.Content>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<custom:Divider x:Name="topDivider"
Grid.Row="0"
Grid.ColumnSpan="2"
BackgroundColor="{StaticResource grayColor}" />
<StackLayout x:Name="container"
Orientation="Vertical"
Spacing="0"
Grid.Column="0"
Margin="15,12,0,12"
Grid.Row="1"
VerticalOptions="Center">
<Label x:Name="headText" />
<Label x:Name="bodyText" />
</StackLayout>
<custom:Divider x:Name="bottomDivider"
Grid.Row="2"
Grid.ColumnSpan="2"
BackgroundColor="{StaticResource grayColor}" />
<ContentView x:Name="asideContainer"
Margin="0,0,15,0"
Grid.Column="1"
Grid.Row="1" />
</Grid>
</ContentView.Content>
</ContentView>
解决方法:
快速查看编译器生成的文件(.xaml.g.cs)-您会注意到这些私有字段是在InitializeComponent方法中分配的:
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Forms.Build.Tasks.XamlG", "0.0.0.0")]
private void InitializeComponent()
{
global::Xamarin.Forms.Xaml.Extensions.LoadFromXaml(this, typeof(BaseSettingsElement));
headText = global::Xamarin.Forms.NameScopeExtensions.FindByName<global::Xamarin.Forms.Label>(this, "headText");
}
因此,这似乎是由于调用InitializeComponent和PropertyChangedHandler的顺序而引起的问题.
在第一种情况下,显式设置属性时,这就是方法的调用顺序.
InitializeComponent -Start
InitializeComponent -End
HeadTextColor -PropertyChanged
在第二种情况下,使用全局样式设置属性时,顺序为:
HeadTextColor -PropertyChanged
InitializeComponent -Start
InitializeComponent -End
看起来该属性是在基类构造函数中的某个位置设置的.因此,NullReferenceException是预期的.
有两种解决方法.
解决方案1(推荐)
与其使用属性更改的处理程序,不如使用绑定来设置内部子控件的属性,并将父节点作为Source.例如:
<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="RemoteControlApp.CustomViews.BaseSettingsElement"
xmlns:custom="clr-namespace:RemoteControlApp.CustomViews;assembly=RemoteControlApp"
x:Name="_parent">
<ContentView.Content>
<!-- .... -->
<Label TextColor="{Binding HeadTextColor, Source={x:Reference _parent}}" />
解决方案2
或者,在InitializeComponent之后设置内部控件的属性.
public MyView()
{
Debug.WriteLine("InitializeComponent -Start");
InitializeComponent();
Debug.WriteLine("InitializeComponent -End");
//InitializeControl();
this.headText.TextColor = HeadTextColor;
}
注意:在第二个解决方案中,您仍然需要更改属性的处理程序.否则,在XAML中显式设置属性不会将更改传播到内部控件.我猜是为了进行空检查.
内容总结
以上是互联网集市为您收集整理的当我尝试将全局样式应用于Xamarin表单中的自定义ContentView时,出现C#-NullReferenceException全部内容,希望文章能够帮你解决当我尝试将全局样式应用于Xamarin表单中的自定义ContentView时,出现C#-NullReferenceException所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。