在代码隐藏崩溃时绑定到 UIElement
Bind to UIElement in code-behind crashing
在我的代码中,我有一个 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 异常。我没有得到任何描述,错误代码每次都会更改。我用断点检查了这种行为是否真的是由绑定引起的,确实如此。
如果您对这里可能出现的问题有任何建议,请 post 回答。我将提供所需的任何其他信息(如果合理,我不会向您发送我的整个项目文件)
感谢任何帮助!
你的问题是你使用的是变量,而不是 属性。
private UIElement currentMenu;
public string CurrentMenu
{
get { return currentMenu; }
set {
currentMenu=value);
OnPropertyChanged("CurrentMenu");
}
}
所以将 Control 绑定到 "varaible" 的基本规则:
- 变量应该是 属性,而不是字段。
- 应该是
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">
我找到问题的答案了!
也就是说,这不是做这件事的方法。相反,我在 SplitView 的内容中声明了一个框架:
<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)!
在我的代码中,我有一个 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 异常。我没有得到任何描述,错误代码每次都会更改。我用断点检查了这种行为是否真的是由绑定引起的,确实如此。
如果您对这里可能出现的问题有任何建议,请 post 回答。我将提供所需的任何其他信息(如果合理,我不会向您发送我的整个项目文件)
感谢任何帮助!
你的问题是你使用的是变量,而不是 属性。
private UIElement currentMenu;
public string CurrentMenu
{
get { return currentMenu; }
set {
currentMenu=value);
OnPropertyChanged("CurrentMenu");
}
}
所以将 Control 绑定到 "varaible" 的基本规则:
- 变量应该是 属性,而不是字段。
- 应该是
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">
我找到问题的答案了!
也就是说,这不是做这件事的方法。相反,我在 SplitView 的内容中声明了一个框架:
<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)!