在WPF中重构XAML [英] Refactor XAML in WPF
本文介绍了在WPF中重构XAML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的XAML已经很长,很难维护。
My XAML has got very long and hard to maintain. I was wondering if there is a way to do something like refactoring?
这里有一个简单的例子:
Here is a simple example:
<Window x:Class="RefactorXAML.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<StackPanel>
<Button>New</Button>
<Button>Open</Button>
</StackPanel>
</Grid>
</Window>
如何重构 Stackpanel
写这样?
<Window x:Class="RefactorXAML.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
// Refactored Markup
</Grid>
</Window>
推荐答案
您可以将这些按钮放在用户控件中,使它像这样:
You could put those buttons in a user control, to make it like this:
<Window …
xmlns:my="clr-namespace:SomeNamespace">
<Grid>
<my:NewAndOpenButtons />
</Grid>
</Window>
您还可以用已经包含这些按钮的项目控件替换网格,
You could also replace the grid by an items control which already includes those buttons, but that way you would impose too many restrictions on the other content, so I wouldn’t do that.
所以对于上面的内容,你的用户控件看起来像这样:
So for the above, your user control would look something like this:
<UserControl x:Class="SomeNamespace.NewAndOpenButtons" …>
<StackPanel>
<Button>New</Button>
<Button>Open</Button>
</StackPanel>
</UserControl>
这篇关于在WPF中重构XAML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文