【问题标题】:Bind to UIElement in code-behind crashing在代码隐藏崩溃中绑定到 UIElement
【发布时间】:2016-09-12 11:39:06
【问题描述】:

在我的代码中,我使用某些按钮设置了一个 UIElement 变量。 现在,我有了这个变量:

public UIElement currentMenu;

设置为这个值:

currentMenu = (UIElement)Resources["Home"];

我从资源中获取它,因此我不必在代码隐藏中乱七八糟地管理它,一旦我解决了这个问题,我会将资源导出到单独的 ResourceDictionary。

我的 SplitView 如下所示:

<SplitView x:Name="NavPane" OpenPaneLength="250" CompactPaneLength="50" Content="{x:Bind currentMenu}" DisplayMode="CompactOverlay" IsPaneOpen="False" PaneClosing="NavPane_PaneClosing">

此时问题出现了,Binding 使整个应用程序崩溃,并出现未处理的 win32 异常。我没有得到任何描述,并且错误代码每次都会更改。我已经用断点检查了这种行为是否实际上是由绑定引起的,并且确实是。

如果您对此处可能出现的问题有任何建议,请发布答案。我将提供所需的任何其他信息(如果合理,我不会将我的整个项目文件发送给您)

感谢任何帮助!

【问题讨论】:

    标签: c# xaml data-binding windows-runtime win-universal-app


    【解决方案1】:

    您使用的是变量而不是属性的问题。

    private UIElement currentMenu;
    public string CurrentMenu
    {
       get { return currentMenu; }
       set { 
             currentMenu=value);
             OnPropertyChanged("CurrentMenu");
           }
    } 
    

    因此,将 Control 绑定到“变量”的基本规则:

    • 变量应该是属性,而不是字段。
    • 应该是public
    • 通知属性(适用于模型类)或依赖属性(适用于视图类)

    要通知 UI,您应该实现 INotifyPropertyChanged:

    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
    

    更新:

    您的Bidning 应如下所示:

    <SplitView x:Name="NavPane" OpenPaneLength="250" CompactPaneLength="50" 
           Content="{Binding CurrentMenu}" DisplayMode="CompactOverlay" IsPaneOpen="False"
           PaneClosing="NavPane_PaneClosing">
    

    【讨论】:

    • 我现在有了这个实现:private UIElement currentMenu; public string CurrentMenu { get { return currentMenu.ToString(); } set { currentMenu = (UIElement) Resources[value]; OnPropertyChanged("CurrentMenu"); } } public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } 仍然返回类似的错误
    • @LightDrake 你能发布什么 innerexception 说和完整的堆栈异常跟踪。
    • @LightDrake 是 UWP 吗?
    • 是的,它是 UWP,但我不确定如何访问错误消息,我得到的(在构建时)是: [projectname].exe [11576] 中出现未处理的 win32 异常]
    【解决方案2】:

    我找到了问题的答案!

    也就是说,这不是这样做的方法。相反,我在 SplitView 的内容中声明了一个 Frame:

    <SplitView.Content>
        <Frame x:Name="activeMenu"/>
    </SplitView.Content>
    

    然后,我使用 Frame.Navigate() 函数将我的菜单加载到框架中:

        public MainPage()
        {
            DataContext = this;
            this.InitializeComponent();
            SetMenu(0);
        }
    
        private void SetMenu(int key)
        {
            switch (key)
            {
                case 0:
                    activeMenu.Navigate(typeof(HomeMenu));
                    break;
                //You can add in as many cases as you need
            }
        }
    

    然后您需要在项目文件中将所需的所有菜单设置为单独的页面。在此示例中,HomeMenu.xaml 包含人们在启动应用程序时看到的菜单的网格。

    这解决了问题,但感谢所有为原始(不幸的是不成功)解决方案做出贡献的人 (StepUp)!

    猜你喜欢
    • 2010-11-19
    • 2015-12-30
    • 1970-01-01
    • 2012-07-15
    • 1970-01-01
    • 1970-01-01
    • 2011-04-09
    • 2023-03-27
    • 2019-06-07
    相关资源
    最近更新 更多