none
同一個Silverlight項目中的多個XAML頁之間是否可以相互切換? RRS feed

答案

  • 不用重命名,只要命名空间不同就可以调用了

    Code Snippet

            private void Button_Click(object sender, RoutedEventArgs e)
            {
                panel1.Children.Clear();

                WebClient c = new WebClient();
                c.OpenReadCompleted += new OpenReadCompletedEventHandler(c_OpenReadCompleted);
                c.OpenReadAsync(new Uri("DynamicXapDataGrid_CS.xap", UriKind.Relative));
            }

            void c_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
            {
                string appManifest = new StreamReader(Application.GetResourceStream(new StreamResourceInfo(e.Result, null), new Uri("AppManifest.xaml", UriKind.Relative)).Stream).ReadToEnd();

                XElement deploy = XDocument.Parse(appManifest).Root;
                List parts = (from assemblyParts in deploy.Elements().Elements()
                                        select assemblyParts).ToList();

                Assembly asm = null;

                foreach (XElement xe in parts)
                {
                    string source = xe.Attribute("Source").Value;

                    AssemblyPart asmPart = new AssemblyPart();

                    StreamResourceInfo streamInfo = Application.GetResourceStream(new StreamResourceInfo(e.Result, "application/binary"), new Uri(source, UriKind.Relative));

                    if (source == "DynamicXapDataGrid_CS.dll")
                    {
                        asm = asmPart.Load(streamInfo.Stream);
                    }
                    else
                    {
                        asmPart.Load(streamInfo.Stream);
                    }
                }

                panel1.DataContext = FakeData.GetCustomers(LastNameSearch.Text);

                UIElement myData = asm.CreateInstance("DynamicXapDataGrid_CS.Page") as UIElement;

                panel1.Children.Add(myData);
                panel1.UpdateLayout();

            }

     

     

    Code Snippet

        <Grid x:Name="LayoutRoot" Background="White">
            <StackPanel>
                <TextBox x:Name="LastNameSearch" Width="200" Height="25" />
                <Button Click="Button_Click" Content="Submit Search" Width="200" Height="25" />
                <StackPanel x:Name="panel1" />
            </StackPanel>
        </Grid>

     

     

    2008年12月8日 13:44
    版主

全部回复

  •  liliLin 写:

    同一個Silverlight項目中的多個XAML頁之間是否可以進行切換?

     

    一般情况下你是要有个主线程,跟win application的概念是一样的,如果不修改就是默认的Page.xaml Page.xaml.cs,然后可以在这个主线程中加载xaml,或者加载usercontrol

    2008年12月3日 14:16
    版主
  •  

    多個XAML頁之間是否可以進行切換是什么意思?你要得是WPF中提供的Frame那种工作方式吗?
    2008年12月5日 10:52
  • 不是。要得是與一般的Web一樣,可以通過各種連接轉到其他的Web頁面。

    即,我在一個Silverlight項目中有多個XAML的Page(已重命名的),然後我在其中一個Page中通過一個事件轉到另一個Page去。

    2008年12月8日 5:12
  • 不用重命名,只要命名空间不同就可以调用了

    Code Snippet

            private void Button_Click(object sender, RoutedEventArgs e)
            {
                panel1.Children.Clear();

                WebClient c = new WebClient();
                c.OpenReadCompleted += new OpenReadCompletedEventHandler(c_OpenReadCompleted);
                c.OpenReadAsync(new Uri("DynamicXapDataGrid_CS.xap", UriKind.Relative));
            }

            void c_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
            {
                string appManifest = new StreamReader(Application.GetResourceStream(new StreamResourceInfo(e.Result, null), new Uri("AppManifest.xaml", UriKind.Relative)).Stream).ReadToEnd();

                XElement deploy = XDocument.Parse(appManifest).Root;
                List parts = (from assemblyParts in deploy.Elements().Elements()
                                        select assemblyParts).ToList();

                Assembly asm = null;

                foreach (XElement xe in parts)
                {
                    string source = xe.Attribute("Source").Value;

                    AssemblyPart asmPart = new AssemblyPart();

                    StreamResourceInfo streamInfo = Application.GetResourceStream(new StreamResourceInfo(e.Result, "application/binary"), new Uri(source, UriKind.Relative));

                    if (source == "DynamicXapDataGrid_CS.dll")
                    {
                        asm = asmPart.Load(streamInfo.Stream);
                    }
                    else
                    {
                        asmPart.Load(streamInfo.Stream);
                    }
                }

                panel1.DataContext = FakeData.GetCustomers(LastNameSearch.Text);

                UIElement myData = asm.CreateInstance("DynamicXapDataGrid_CS.Page") as UIElement;

                panel1.Children.Add(myData);
                panel1.UpdateLayout();

            }

     

     

    Code Snippet

        <Grid x:Name="LayoutRoot" Background="White">
            <StackPanel>
                <TextBox x:Name="LastNameSearch" Width="200" Height="25" />
                <Button Click="Button_Click" Content="Submit Search" Width="200" Height="25" />
                <StackPanel x:Name="panel1" />
            </StackPanel>
        </Grid>

     

     

    2008年12月8日 13:44
    版主