通过一个代码控制多个XAML文件

时间:2017-12-29 13:02:04

标签: wpf xaml ironpython

我制作了2个XAML文件。

当我打开程序并且它包含一个按钮时,第一个被加载。

XAML(WpfApplication4.xaml):

<Window 
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       Title="WpfApplication4" Height="300" Width="300">
    <Grid>
        <Button x:Name="Button" Content="Button" HorizontalAlignment="Left" Height="86" Margin="47,68,0,0" VerticalAlignment="Top" Width="184" Click="Button_Click"/>
    </Grid>
</Window>

第二个XAML(称为addon.xaml)包含一个按钮和一个列表。

XAML(addon.xaml):

<Window 
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       Title="addon" Height="300" Width="300">
    <Grid>
        <Button x:Name="Button2" Content="Button" HorizontalAlignment="Left" Height="86" Margin="47,68,0,0" VerticalAlignment="Top" Width="184" Click="Button2_Click"/>
        <ListBox HorizontalAlignment="Left" Height="59" Margin="168,177,0,0" VerticalAlignment="Top" Width="81"/>
        <Button Content="Button" HorizontalAlignment="Left" Height="13" Margin="242,25,0,0" VerticalAlignment="Top" Width="23"/>
        <Label Content="test" HorizontalAlignment="Left" Height="38" Margin="80,25,0,0" VerticalAlignment="Top" Width="99"/>
    </Grid>
</Window>

我的目标是在单击按钮以切换屏幕时切换XAML文件(现在它只包含按钮和列表,但通常它应该是与第一个屏幕截然不同的屏幕)。

.py看起来如下:( EDITED

import wpf

from System.Windows import Application, Window

class MyWindow(Window):
    def __init__(self):
        self.ui = wpf.LoadComponent(self, 'WpfApplication4.xaml')

def Button_Click(self, sender, e):
    self.ui = wpf.LoadComponent(self, 'addon.xaml')

def Button2_Click(self, sender, e):
    self.ui = wpf.LoadComponent(self, 'WpfApplication4.xaml')

if __name__ == '__main__':
    Application().Run(MyWindow())

然后它设法在XAML之间切换,但是,当我点击button2返回到原始的XAML(WpfApplication4)它说已经存在一个名为button的变量(这是显而易见的,因为我每次都无法更改按钮名称它被点击)

有没有办法“删除”“自我”的记忆,所以它不会记得第一次打开时的变量?

Could not register named object. Cannot register duplicate name 'Button' in this scope.

谢谢

0 个答案:

没有答案