[英]Null values when I try to reach Rectangle
I have a uwp project that i have a booking function in and would be pleased to have some help with one thing. 我有一个uwp项目,我具有预订功能,很高兴在某件事上有帮助。
So i'm trying to reach a Rectangle
in my UserControl
from my Main.xaml.cs
file, so i can set the Rectangles
fill property. 因此,我试图从
Main.xaml.cs
文件中的UserControl
到达Rectangle
,以便设置Rectangles
填充属性。
This is how i have done it: 这就是我的做法:
Method in Main.xaml.cs
for finding the child property: Main.xaml.cs
用于查找子属性的方法:
private DependencyObject FindChildControl<T>(DependencyObject control, string ctrlName)
{
int childNumber = VisualTreeHelper.GetChildrenCount(control);
for (int i = 0; i < childNumber; i++)
{
DependencyObject child = VisualTreeHelper.GetChild(control, i);
FrameworkElement fe = child as FrameworkElement;
// Not a framework element or is null
if (fe == null) return null;
if (child is T && fe.Name == ctrlName)
{
// Found the control so return
return child;
}
else
{
// Not found it - search children
DependencyObject nextLevel = FindChildControl<T>(child, ctrlName);
if (nextLevel != null)
return nextLevel;
}
}
return null;
}
Inside the function where i want to call the Rectangle
that is named StatusColor
I do this: 在要调用名为
StatusColor
的Rectangle
的函数中,我这样做:
Rectangle Rec = FindChildControl<Rectangle>(GridView1, "StatusColor") as Rectangle;
But somehow when I try to execute the code I get this error for the variable Rec
: 但是以某种方式,当我尝试执行代码时,对于变量
Rec
收到此错误:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
System.NullReferenceException:'对象引用未设置为对象的实例。
I know there are many posts about this exception but i still not getting any smarter by looking at them... 我知道有很多关于此异常的帖子,但是通过查看它们,我仍然没有变得更聪明...
But as i said, I would be very pleased with someones help 但正如我所说,我会很高兴有人帮忙
Thanks in advance! 提前致谢!
EDIT: 编辑:
My xaml
in Main.xaml
to declare the UserControl
: 我在
Main.xaml
xaml
声明了UserControl
:
<GridView x:Name="GridView1" ItemContainerStyle="{StaticResource testgrid}" ItemsSource="{x:Bind Rooms}" Width="1740" Height="835" IsHitTestVisible="False" ScrollViewer.HorizontalScrollBarVisibility="Hidden" Margin="75,190,75,100" FontFamily="Segoe MDL2 Assets" IsDoubleTapEnabled="False" IsHoldingEnabled="False" IsRightTapEnabled="False" IsTapEnabled="False">
<GridView.ItemTemplate>
<DataTemplate x:DataType="data:Room">
<local:TemplateGrid x:Name="TemplateGrid"/>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
And this is how my code looks like for the Rectangle
: 这就是我的
Rectangle
代码的样子:
<Rectangle x:Name="StatusColor" x:FieldModifier="public" Margin="0,0,0,0" Height="218" VerticalAlignment="Top">
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#00000000" Offset="0.901"/>
<GradientStop Color="#CC17FF00" Offset="0"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
It is these lines: 这些行是:
// Not a framework element or is null
if (fe == null) return null;
You are exiting the loop too early. 您太早退出循环了。 When the element
i
is not a framework element, you should just ignore it and continue on to the next one. 当元素
i
不是框架元素时,您应该忽略它并继续进行下一个。
So instead of exiting when you encounter a non-framework element, just execute the next lines only when it is one: 因此,当遇到非框架元素时,不要退出,而仅在它是一个元素时才执行下一行:
if (fe != null)
{
// rest of checking
}
or alternatively: 或者:
if (fe == null) continue;
EDIT 编辑
A slight reworking of your code to: 稍微修改一下代码即可:
private T FindChildControl<T>(DependencyObject control, string ctrlName)
where T: FrameworkElement
{
int childNumber = VisualTreeHelper.GetChildrenCount(control);
for (int i = 0; i < childNumber; i++)
{
DependencyObject child = VisualTreeHelper.GetChild(control, i);
FrameworkElement fe = child as FrameworkElement;
// Not a framework element or is null
if (fe == null)
{
continue;
}
if (child is T && fe.Name == ctrlName)
{
// Found the control so return
return (T)child;
}
else
{
// Not found it - search children
T nextLevel = this.FindChildControl<T>(child, ctrlName);
if (nextLevel != null)
{
return nextLevel;
}
}
}
return null;
}
should be called with 应该与
Rectangle rec = FindChildControl<Rectangle>(GridView1, "StatusColor");
to give the correct result. 给出正确的结果。 At least it did in my test situation (with adjusted parameters).
至少在我的测试情况下(使用调整后的参数)确实如此。
The only real change was the use of continue
instead of return
. 唯一真正的变化是使用
continue
而不是return
。 Furthermore I made use of generics to immediately return the correct type, no casting needed. 此外,我利用泛型立即返回正确的类型,无需强制转换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.