首页 新闻 会员 周边

WPF 获取 XAML

0
悬赏园豆:80 [已解决问题] 解决于 2013-11-12 16:07

我想问一下WPF中可否像html一样,获取所有的xaml对象,包括后台代码动态添加的控件对象,并且可以通过获取到的xaml加载到页面中的方法谢谢。

可以加分

WPF
邵明瑞的主页 邵明瑞 | 初学一级 | 园豆:105
提问于:2013-10-30 15:51
< >
分享
最佳答案
0

获取所有的控件:

private void btnLoadXAML_Click(object sender, RoutedEventArgs e)
{
    try
    {
        OpenFileDialog Fd = new OpenFileDialog();
        Fd.ShowDialog();
        string LoadedFileName = Fd.FileName;

        //加载XAML文件
        FileStream Fs = new FileStream(@LoadedFileName, FileMode.Open);
        Grid grdToLoad = new Grid();
        grdToLoad.Height = 210;
        grdToLoad.Width = 400;

        grdToLoad = System.Windows.Markup.XamlReader.Load(Fs) as Grid;

        grdLoadXAML.Children.Add(grdToLoad);

        Fs.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

 

这是把获取的代码加载到XAML文件中:

private void btnCreateXAML_Click(object sender, RoutedEventArgs e)
{
    try
    {
        Grid grd = new Grid();
        grd.Height = 210;
        grd.Width = 400;
        Button btn = new Button();
        btn.Height = 50;
        btn.Width = 80;
        btn.Content = "Dyn. Button";
        btn.Background = new SolidColorBrush(Colors.Red);
        btn.Margin = new Thickness(5, 5, 310, 120);
        grd.Children.Add(btn);

        TextBox txt = new TextBox();
        txt.Height = 50;
        txt.Width = 100;
        txt.Text = "Dynamic TextBox";
        txt.Foreground = new SolidColorBrush(Colors.Red);
        txt.Margin = new Thickness(5, 60, 310, 80);
        grd.Children.Add(txt);

        //保存Xaml文件

        FileStream Fs = new FileStream(@"H:\GeneratedFiles\MyDesign.Xml",
            FileMode.CreateNew);
        System.Windows.Markup.XamlWriter.Save(grd, Fs);
        Fs.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

 

大致思路就是这些了,你根据你的需要改动一下。

收获园豆:50
悟行 | 专家六级 |园豆:12559 | 2013-10-31 12:57

先谢谢,不过看第一段代码的意思貌似没有读当前窗体的xaml,我试试。谢谢。

邵明瑞 | 园豆:105 (初学一级) | 2013-10-31 13:11

@邵明瑞: 你把路径改成你的当前xaml的路径。

悟行 | 园豆:12559 (专家六级) | 2013-10-31 14:33

大神,这种方式System.Windows.Markup.XamlWriter.Save(grd, Fs);获取不到例如Button中的Click属性,这种情况怎么获取,谢谢指点

侯小北 | 园豆:39 (初学一级) | 2018-11-01 17:35
其他回答(1)
0

myxaml1.txt内容

 <Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <Button Height="55" Margin="100,115,0,0" Name="button1" VerticalAlignment="Top" HorizontalAlignment="Left" Width="142">Button</Button>
        <Button Height="55" Margin="291,115,166,0" Name="button2" VerticalAlignment="Top">Button</Button>
 </Grid>

获取文件内容,并且加载到页面

        /// <summary>
        /// 动态添加控件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, RoutedEventArgs e)
        {
          DependencyObject bto = new Button();
          Button bt = (Button)bto;
          bt.Margin = new Thickness(2,2,0,0);
          bt.Width = 100;
          bt.Height = 100;
          bt.Content = "hello";
          g1.Children.Add(bt);          
        }
        /// <summary>
        /// 动态加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, RoutedEventArgs e)
        {
            Stream sm = File.OpenRead(@"myxaml1.txt");
            DependencyObject element = (DependencyObject)XamlReader.Load(sm);
            this.Content = element;
        }

收获园豆:30
贪心狸猫 | 园豆:872 (小虾三级) | 2013-10-30 21:08

万分感谢!

支持(0) 反对(0) 邵明瑞 | 园豆:105 (初学一级) | 2013-10-31 11:48

获取呢。

支持(0) 反对(0) 邵明瑞 | 园豆:105 (初学一级) | 2013-10-31 11:49
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册