![](/img/trans.png)
[英]Binding to the DataContext within a Style results in a 'catastrophic failure?'
[英]Catastrophic failure in xaml binding
我正在開發Windows 10通用應用程序。 我有下面的代碼:
XAML:
<Page
x:Class="MyProject.BlankPage1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyProject"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
>
<ItemsControl ItemsSource="{Binding}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas Background="Purple"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Canvas.Left" Value="{Binding X}"/>
<Setter Property="Canvas.Top" Value="{Binding Y}"/>
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Rectangle Fill="Red" Width="50" Height="50"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Page>
和后面的代碼:
namespace MyProject
{
public sealed partial class BlankPage1 : Page
{
public BlankPage1()
{
DataContext =
new[]
{
new { X = 50.0, Y = 100.0 },
new { X = 220.0, Y = 170.0 }
};
InitializeComponent();
}
}
}
不幸的是,這些矩形不會顯示在窗口中。 我收到編譯錯誤:
Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))
分配給Canvas的xaml靜態數字坐標可以正常工作。
為什么會發生錯誤而代碼不起作用?
我在構建通用Windows平台應用程序時偶然發現了這個問題。
做了一些谷歌搜索,找到了這篇文章。
這非常有幫助。 我將他的SetterValueBindingHelper
類復制到了自己的項目中。 之后,我做了1次調整,因為
type = System.Type.GetType(item.Type).GetTypeInfo();
在XAML綁定中執行Type="Canvas"
時,給出了異常。 它首先嘗試在當前程序集中找到類Canvas
。 這將返回null
,然后調用.GetTypeInfo()
並.GetTypeInfo()
NullReferenceException。
在其上實現了C#6.0的新Null-Conditional Operator
,從而解決了此問題。 之后,代碼立即檢查type
是否為null,然后遍歷所有已加載的程序集以找到Canvas
。
type = System.Type.GetType(item.Type)?.GetTypeInfo();
他的第二個用法示例奇怪地與將其與Canvas
元素一起使用有關。
我的項目VisualDesigner中的另一個示例:
這是基於他的示例的最終XAML:
<ItemsControl Grid.Column="1" ItemsSource="{Binding CanvasItems}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="helpers:SetterValueBindingHelper.PropertyBinding">
<Setter.Value>
<helpers:SetterValueBindingHelper>
<helpers:SetterValueBindingHelper Type="Canvas" Property="Left" Binding="{Binding WindowX, Mode=TwoWay}" />
<helpers:SetterValueBindingHelper Type="Canvas" Property="Top" Binding="{Binding WindowY, Mode=TwoWay}" />
</helpers:SetterValueBindingHelper>
</Setter.Value>
</Setter>
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<applicationwindow:ApplicationWindow Width="{Binding WindowWidth}" Height="{Binding WindowHeight}" DataContext="{Binding}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
helpers:
指向SetterValueBindingHelper
所在的名稱空間。在我的情況下, ApplicationWindow
是自定義UserControl。 CanvasItems
是一個ObservableCollection<ApplicationWindowViewModel>
,這是我的ApplicationWindowViewModel
:
[ImplementPropertyChanged]
public class ApplicationWindowViewModel : ViewModelBase
{
public string Title { get; set; }
public double WindowX { get; set; } = 10;
public double WindowY { get; set; } = 10;
public int WindowWidth { get; set; } = 300;
public int WindowHeight { get; set; } = 200;
}
在此示例中,我使用Fody.PropertyChanged處理X / Y / Width / Height屬性上的屬性更改事件,如果您不使用此包,請不要忘記實現自己的PropertyChanged事件處理程序,等等。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.