【WPF】wpf加载和保存xaml文件

html-css011

【WPF】wpf加载和保存xaml文件,第1张

加载效果图:

public MainWindow(string xamlFile)

        {

            InitializeComponent()

            DependencyObject rootElement

            using (FileStream fs = new FileStream(xamlFile, FileMode.Open))

            {

                rootElement = (DependencyObject)XamlReader.Load(fs)

            }

            Content = rootElement

            //查找控件(方法一)

            button1 = (Button) LogicalTreeHelper.FindLogicalNode(rootElement, "button1")

修改app.xaml.cs里的启动项

保存功能:

换成Create可以覆盖保存文件,不会重复而报错。

我写了个例子,我用的是Canvas,设置ClipToBounds = true可以隐藏不需要显示的部分,也就是100×100方块外的部分,每1秒设置Image的Canvas的Left和Top属性值即可,我的Project是VS2012写的,你可以把里面的代码拷出来在VS2005或VS2010中粘贴即可。