![](/img/trans.png)
[英]Getting a SqlException when I try to insert null values through a stored procedure
[英]Null values when I try to reach Rectangle
我有一個uwp項目,我具有預訂功能,很高興在某件事上有幫助。
因此,我試圖從Main.xaml.cs
文件中的UserControl
到達Rectangle
,以便設置Rectangles
填充屬性。
這就是我的做法:
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;
}
在要調用名為StatusColor
的Rectangle
的函數中,我這樣做:
Rectangle Rec = FindChildControl<Rectangle>(GridView1, "StatusColor") as Rectangle;
但是以某種方式,當我嘗試執行代碼時,對於變量Rec
收到此錯誤:
System.NullReferenceException:'對象引用未設置為對象的實例。
我知道有很多關於此異常的帖子,但是通過查看它們,我仍然沒有變得更聰明...
但正如我所說,我會很高興有人幫忙
提前致謝!
編輯:
我在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>
這就是我的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>
這些行是:
// Not a framework element or is null
if (fe == null) return null;
您太早退出循環了。 當元素i
不是框架元素時,您應該忽略它並繼續進行下一個。
因此,當遇到非框架元素時,不要退出,而僅在它是一個元素時才執行下一行:
if (fe != null)
{
// rest of checking
}
或者:
if (fe == null) continue;
編輯
稍微修改一下代碼即可:
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;
}
應該與
Rectangle rec = FindChildControl<Rectangle>(GridView1, "StatusColor");
給出正確的結果。 至少在我的測試情況下(使用調整后的參數)確實如此。
唯一真正的變化是使用continue
而不是return
。 此外,我利用泛型立即返回正確的類型,無需強制轉換。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.