绑定到UIElement代码隐藏崩溃 [英] Bind to UIElement in code-behind crashing

查看:157
本文介绍了绑定到UIElement代码隐藏崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  public UIElement currentMenu; 

设置为此值:

  currentMenu =(UIElement)资源[Home]; 

我从资源中得到它,所以我不必在代码隐藏中麻烦地管理它一旦我解决了这个问题,我将把资源导出到一个单独的ResourceDictionary。



我的SplitView如下所示:



OpenPaneLength =250CompactPaneLength =50Content ={x:Bind currentMenu}DisplayMode =CompactOverlayIsPaneOpen = FalsePaneClosing =NavPane_PaneClosing>

此时,这个标志就出现了,Binding以一个unshdled的win32异常崩溃了整个应用程序。我没有任何描述,错误代码每次更改。我已经检查了断点,这个行为是否真的是由绑定造成的,而且是。



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



任何帮助不胜感激!

解决方案

我已经找到了我的问题的答案!



也就是说,这不是去做吧。相反,我在SplitView的内容中声明了一个框架:

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

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

  public MainPage()
{
DataContext = this;
this.InitializeComponent();
SetMenu(0);
}

private void SetMenu(int key)
{
switch(key)
{
case 0:
activeMenu .Navigate(typeof(HomeMenu));
break;
//您可以根据需要添加多少个案例
}
}

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



这解决了问题,但谢谢大家(StepUp)谁促成了原始(不幸的是不成功)解决方案!


In my code, I have an UIElement variable I set with certain button presses. Now, I have this variable:

public UIElement currentMenu;

Set to this value:

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

I get it from the Resources so I don't have to manage it messily in the code-behind, I will export the Resources to a seperate ResourceDictionary once I get this problem solved.

My SplitView looks like this:

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

The prblem comes in at this point, the Binding crashes the entire application with an unhndled win32 exception. I get no description and the error code changes every time. I have checked with break points whether this behaviour is actually caused by the binding, and it is.

If you have any suggestions on what might be going wrong here, please post an answer. I will supply any additional information needed (if reasonable, I'm not going to send you my entire project files)

Any help is appreciated!

解决方案

I have found the answer to my question!

Namely, this was not the way to do it. Instead, I declared a Frame inside the Content of the SplitView:

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

Then, I use the Frame.Navigate() function to load my menus into the Frame:

    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
        }
    }

What you then need is to set all the menus you want as separate Pages within your project files. in this example, HomeMenu.xaml contains the grid for the menu people see upon starting up the app.

This solved the problem, but thank you to everyone (StepUp) who contributed to the original (unfortunately unsuccessful) solution!

这篇关于绑定到UIElement代码隐藏崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆