简体   繁体   English

从XAML读取XAML文件

[英]reading xaml file from xaml

I have this situation. 我有这种情况。

http://i.stack.imgur.com/iEIys.png http://i.stack.imgur.com/iEIys.png

I have such a window, where the names of Topics are beiing generated from .xaml files, like this http://i.stack.imgur.com/e2XMP.png the names of topics are being generated by this code 我有一个这样的窗口,其中的主题名称是从.xaml文件生成的,例如http://i.stack.imgur.com/e2XMP.png ,此代码正在生成主题的名称

var fileNames = Directory.GetFiles(Environment.CurrentDirectory, "Topic?.xaml");
        this.Topics = new List<Topic>();
        foreach (var fileName in fileNames)
        {
            var topic = new Topic();
            using (FileStream fs = new FileStream(fileName, FileMode.Open))
            {
                var page = (Page)XamlReader.Load(fs);
                topic.Page = page;
            }

            this.Topics.Add(topic);
        }

Now, I want when the appropriate Topic is clicked, for example Topic 2, the topic2.xaml is opening in new window, and if 3-rd is clicked the topic3.xaml is showing on new window. 现在,我想单击适当的主题(例如主题2),然后在新窗口中打开topic2.xaml,如果单击3rd,则在新窗口中显示topic3.xaml。

Thanks guys for any help. 谢谢大家的帮助。

You could try to do as shown here using XamlReader 你可以尝试做如图所示这里使用XamlReader

using(var mystr = File.Open(fileName))
     DependencyObject rootObject = XamlReader.Load(mystr) as DependencyObject;

However, this does not sound like a good pattern to me :/ 但是,这听起来对我来说不是一个好模式:/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM