Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.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# 无法将双向模式设置为x:Bind_C#_Xaml_Binding_Uwp_Windows 10 Universal - Fatal编程技术网

C# 无法将双向模式设置为x:Bind

C# 无法将双向模式设置为x:Bind,c#,xaml,binding,uwp,windows-10-universal,C#,Xaml,Binding,Uwp,Windows 10 Universal,我需要将UI元素的位置保存到变量。我将x和y放在我的收藏中并绑定它 <Canvas> <Grid Canvas.Top="{x:Bind PositionY, Mode=TwoWay}" Canvas.Left="{x:Bind PositionX, Mode=TwoWay}" 我在页面上通过移动来更改它,并尝试在集合中更新这些内容 但是如果我设置了Mode=TwoWay,我就有编译错误 严重性代码说明项目文件行抑制状态 错误CS1061“网格”

我需要将UI元素的位置保存到变量。我将
x
y
放在我的收藏中并绑定它

  <Canvas>
   <Grid
     Canvas.Top="{x:Bind PositionY, Mode=TwoWay}"
     Canvas.Left="{x:Bind PositionX, Mode=TwoWay}"
我在页面上通过移动来更改它,并尝试在集合中更新这些内容 但是如果我设置了
Mode=TwoWay
,我就有编译错误

严重性代码说明项目文件行抑制状态 错误CS1061“网格”不包含“顶部”的定义,并且没有 接受类型为“Grid”的第一个参数的扩展方法“Top”可能会失败 被发现


这是一个编译器问题,已在Windows 10周年更新SDK(14393)中修复

正如我们所知,使用生成的代码来实现其好处。在XAML编译时,
{x:Bind}
被转换成代码,该代码将从数据源的属性中获取一个值,并在标记中指定的属性上设置该值

当应用程序针对14393之前的版本时,它将生成如下代码来更新双向绑定:

this.obj2 = (global::Windows.UI.Xaml.Controls.Grid)target;
(this.obj2).RegisterPropertyChangedCallback(global::Windows.UI.Xaml.Controls.Canvas.LeftProperty,
    (global::Windows.UI.Xaml.DependencyObject sender, global::Windows.UI.Xaml.DependencyProperty prop) =>
    {
        if (this.initialized)
        {
            // Update Two Way binding
            this.dataRoot.PositionX = (this.obj2).Left;
        }
    });
(this.obj2).RegisterPropertyChangedCallback(global::Windows.UI.Xaml.Controls.Canvas.TopProperty,
    (global::Windows.UI.Xaml.DependencyObject sender, global::Windows.UI.Xaml.DependencyProperty prop) =>
    {
        if (this.initialized)
        {
            // Update Two Way binding
            this.dataRoot.PositionY = (this.obj2).Top;
        }
    });
obj2
是一个
网格
,它不包含名为
Left
Top
的属性,因此我们将得到编译器错误

要解决此问题,应用程序的最低目标SDK版本必须为14393或更高版本。要更改已在Visual Studio中创建的项目的最低版本和目标版本,请转到项目→ 性质→ 应用程序选项卡→ 目标定位

在这之后,我们可以重建项目,然后应该没有编译器错误。应该正确生成绑定

this.obj2 = (global::Windows.UI.Xaml.Controls.Grid)target;
(this.obj2).RegisterPropertyChangedCallback(global::Windows.UI.Xaml.Controls.Canvas.LeftProperty,
    (global::Windows.UI.Xaml.DependencyObject sender, global::Windows.UI.Xaml.DependencyProperty prop) =>
    {
        if (this.initialized)
        {
            // Update Two Way binding
            this.dataRoot.PositionX = global::Windows.UI.Xaml.Controls.Canvas.GetLeft(this.obj2);
        }
    });
(this.obj2).RegisterPropertyChangedCallback(global::Windows.UI.Xaml.Controls.Canvas.TopProperty,
    (global::Windows.UI.Xaml.DependencyObject sender, global::Windows.UI.Xaml.DependencyProperty prop) =>
    {
        if (this.initialized)
        {
            // Update Two Way binding
            this.dataRoot.PositionY = global::Windows.UI.Xaml.Controls.Canvas.GetTop(this.obj2);
        }
    }); 

你能分享一个能重现你的问题的例子吗?@JayZuo MSFT是的,我知道你为什么要这么做?网格从不更新Canval.Left rest Canvas.Top-attached属性。将它们绑定到源属性(PositionX resp PositionY)后,应仅更改这些源属性properties@Smile:拖放时,不应更新Canvas.Left,但ViewModel.PositionX和Canvas.Left比自动更新更重要。绑定属性后,不要手动设置它们。
this.obj2 = (global::Windows.UI.Xaml.Controls.Grid)target;
(this.obj2).RegisterPropertyChangedCallback(global::Windows.UI.Xaml.Controls.Canvas.LeftProperty,
    (global::Windows.UI.Xaml.DependencyObject sender, global::Windows.UI.Xaml.DependencyProperty prop) =>
    {
        if (this.initialized)
        {
            // Update Two Way binding
            this.dataRoot.PositionX = global::Windows.UI.Xaml.Controls.Canvas.GetLeft(this.obj2);
        }
    });
(this.obj2).RegisterPropertyChangedCallback(global::Windows.UI.Xaml.Controls.Canvas.TopProperty,
    (global::Windows.UI.Xaml.DependencyObject sender, global::Windows.UI.Xaml.DependencyProperty prop) =>
    {
        if (this.initialized)
        {
            // Update Two Way binding
            this.dataRoot.PositionY = global::Windows.UI.Xaml.Controls.Canvas.GetTop(this.obj2);
        }
    });