简体   繁体   English

BindingExpression 路径错误:'SystemParameters'

[英]BindingExpression path error: 'SystemParameters'

While updating my software, I started facing this BindingExpression error.在更新我的软件时,我开始遇到这个BindingExpression错误。

System.Windows.Data Error: 40: BindingExpression path error: 'SystemParameters' property not found on 'object' ''MainViewModel' (HashCode=4781813)'. System.Windows.Data 错误:40:BindingExpression 路径错误:在“对象”“MainViewModel”(HashCode = 4781813)上找不到“SystemParameters”属性。 BindingExpression:Path=SystemParameters.PrimaryScreenHeight; BindingExpression:Path=SystemParameters.PrimaryScreenHeight; DataItem='MainViewModel' (HashCode=4781813); DataItem='MainViewModel' (HashCode=4781813); target element is 'MainWindow' (Name='XXX');目标元素是'MainWindow'(名称='XXX'); target property is 'Height' (type 'Double')目标属性是“高度”(类型“双”)

System.Windows.Data Error: 40: BindingExpression path error: 'SystemParameters' property not found on 'object' ''MainViewModel' (HashCode=4781813)'. System.Windows.Data 错误:40:BindingExpression 路径错误:在“object”“MainViewModel”(HashCode=4781813)上找不到“SystemParameters”属性。 BindingExpression:Path=SystemParameters.PrimaryScreenWidth; BindingExpression:Path=SystemParameters.PrimaryScreenWidth; DataItem='MainViewModel' (HashCode=4781813); DataItem='MainViewModel' (HashCode=4781813); target element is 'MainWindow' (Name='XXX');目标元素是'MainWindow'(名称='XXX'); target property is 'Width' (type 'Double')*目标属性是“宽度”(类型“双”)*

Reading the above error, it seems that it couldn't find the SystemParameters object in MainViewModel .阅读上述错误,似乎在MainViewModel中找不到SystemParameters object 。

<Window x:Name="XXXX" x:Class="XXXX.Views.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:XXXX"
    xmlns:localize="http://gu.se/Localization"
    xmlns:properties="clr-namespace:XXXX.Properties"
    mc:Ignorable="d"
    d:DesignHeight="1080" d:DesignWidth="1920"
    Title=""
    WindowStyle="None"
    ResizeMode="NoResize"
    WindowStartupLocation="CenterScreen" WindowState="{Binding WindowState, Mode=TwoWay}"
    DataContext="{Binding Main, Source ={StaticResource Locator}}"
    Height="{Binding SystemParameters.PrimaryScreenHeight}" 
    Width="{Binding SystemParameters.PrimaryScreenWidth}" Background="#FFCCCCCC">

   <Window.Resources>

When I start my application, the Initializer.cs class is called which initializes all the parameters and creates an instance of MainWindow .当我启动我的应用程序时,会调用Initializer.cs class 来初始化所有参数并创建MainWindow的一个实例。 By debugging, I discovered that the issue seems to appear when I call the method window.Show() and then my application crashes instantly.通过调试,我发现当我调用方法window.Show()时似乎出现了问题,然后我的应用程序立即崩溃。

MainWindow window = new MainWindow();
window.Show();

A Binding will resolve the property path using the DataContext of an element by default (which is inherited).默认情况下, Binding将使用元素的DataContext解析属性路径(这是继承的)。 The PrimaryScreenHeight and PrimaryScreenWidth properties are static and part of the type SystemParameters , which is a static type, that is not part of the data context. PrimaryScreenHeightPrimaryScreenWidth属性是staticSystemParameters类型的一部分,它是static类型,它不是数据上下文的一部分。 To solve the issue you can:要解决此问题,您可以:

  • Use the x:Static markup extension to refer to static properties.使用x:Static标记扩展来引用 static 属性。

    Implements a markup extension that returns static field and property references .实现返回static 字段和属性引用的标记扩展。

     Height="{x:Static SystemParameters.PrimaryScreenHeight}" Width="{x:Static SystemParameters.PrimaryScreenWidth}"
  • If you need to apply a value converter, you can use the x:Static markup extension for the Source .如果您需要应用值转换器,您可以对Source使用x:Static标记扩展。

     Height="{Binding Source={x:Static SystemParameters.PrimaryScreenHeight}, Converter={StaticResource MyConverter}}" Width="{Binding Source={x:Static SystemParameters.PrimaryScreenWidth}, Converter={StaticResource MyConverter}}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM