Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 子类xaml中父类的公共对象为null_C#_Xaml_Uwp_Nullreferenceexception_Derived Class - Fatal编程技术网

C# 子类xaml中父类的公共对象为null

C# 子类xaml中父类的公共对象为null,c#,xaml,uwp,nullreferenceexception,derived-class,C#,Xaml,Uwp,Nullreferenceexception,Derived Class,考虑以下简单的UserControl MyUserControl1.xaml: 我得到了NullReferenceException,它基本上告诉我MyTextBox是空的!!这里怎么了 附言 我花了足够的时间来解决这个问题,并且得出结论,当我从另一个视图派生xaml视图并尝试访问内部公共对象时,就会出现这个问题。 MyUserControl1.xaml和MyUserControl2都有自己的代码隐藏在.cs文件后面,它们只调用InitializeComponent,其他什么都没有。 通过咨询我

考虑以下简单的UserControl MyUserControl1.xaml:

我得到了NullReferenceException,它基本上告诉我MyTextBox是空的!!这里怎么了

附言

我花了足够的时间来解决这个问题,并且得出结论,当我从另一个视图派生xaml视图并尝试访问内部公共对象时,就会出现这个问题。 MyUserControl1.xaml和MyUserControl2都有自己的代码隐藏在.cs文件后面,它们只调用InitializeComponent,其他什么都没有。
通过咨询我的团队,简短的答案不受支持

看起来您希望在要使用的基本XAML文件中具有UI的情况下使用XAML进行可视化继承。对于XAML,这不是Winforms可能支持的收件箱。以下是有关它的更多信息以及限制:


如果在没有任何XAML的情况下创建用户控件已完成的代码,则子类应该可以很好地工作。

我认为这应该可以工作,因为所有控件都作为内部属性添加到UC中。。但事实并非如此!我也想知道为什么?!您能简单地在子类(如basic c)中调用基类的base.InitializeComponent之类的东西吗?
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Test.CustomControls.MyUserControl1"
x:Name="control" Width="250" Height="100">
    <Grid x:Name="MyGrid" x:FieldModifier="public">
        <TextBlock x:Name="MyTextBox" x:FieldModifier="public" Text="Hello from the other side !!" FontWeight="Light" Foreground="red"/>
    </Grid>
</UserControl>
<local:MyUserControl1
x:Class="Test.CustomControls.MyUserControl2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Test.CustomControls">

</local:MyUserControl1>
CustomControls.MyUserControl2 control = new CustomControls.MyUserControl2();
MyGrid.Children.Add(control);
control.MyTextBox.Text = "Some text";//NullReferenceException here