XAML绑定的灾难性失败 [英] Catastrophic failure in xaml binding
问题描述
我正在开发Windows 10 Universal App。我的代码如下:
I am developing Windows 10 Universal App. I have code below:
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();
}
}
}
不幸的是,矩形不会显示在窗口中。我遇到编译错误:
Unfortunatelly, the rectangles does not show in the window. I am getting compilation error:
Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))
分配给画布可协调xaml中的静态数字按预期工作。
Assigning to Canvas coordinates static numbers in xaml works as expected.
为什么出错
推荐答案
我只是在构建通用Windows平台应用程序时偶然发现了此问题。
I just stumbled upon this issue myself, while building a Universal Windows Platform app.
做了一些谷歌搜索,发现这篇文章。
Did some googling, found this article.
它非常有帮助。我将他的 SetterValueBindingHelper
类复制到我自己的项目中。之后,我做了1次调整,因为
It was very helpful. I copied his SetterValueBindingHelper
class into my own project. After that, I made 1 adjustment, because
type = System.Type.GetType(item.Type).GetTypeInfo();
在执行 Type = Canvas $ c时给出异常$ c>在XAML绑定中。它首先尝试在当前程序集中找到类
Canvas
。这将返回 null
,然后调用 .GetTypeInfo()
引发NullReferenceException。
gave an exception when you do Type="Canvas"
in the XAML binding. It first tries to find the class Canvas
in the current assembly. This returns null
, and then it calls .GetTypeInfo()
throwing a NullReferenceException.
在其上实现了C#6.0的新 Null-Conditional运算符
,从而解决了此问题。紧接着检查 type
是否为空的代码,然后遍历所有已加载的程序集以找到 Canvas
。
Implemented C# 6.0's new Null-Conditional Operator
on it, and that solved this issue. The code immediately after checks if type
is null, and then goes through all loaded assemblies to find the Canvas
.
type = System.Type.GetType(item.Type)?.GetTypeInfo();
他的第二个用法示例与将其与 Canvas
元素。
His second usage example is oddly specifically related to using it with a Canvas
element.
我的Project VisualDesigner中的另一个示例:
Another example from my Project VisualDesigner:
这是基于他的示例的我的最终XAML:
Here's my final XAML, based on his example:
<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
:
helpers:
refers to the namespace the SetterValueBindingHelper
is in. ApplicationWindow
in my case is a custom UserControl. CanvasItems
is an ObservableCollection<ApplicationWindowViewModel>
, and this is my 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事件处理程序等。
In this example I use Fody.PropertyChanged to handle the property changed events on the X/Y/Width/Height properties, if you're not using this package, don't forget to implement your own PropertyChanged event handlers, etc.
这篇关于XAML绑定的灾难性失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!