none
how can I publish a event from usercontrol to the other memebers of composeparts?

    Question

  • hi,I've been doing this for couple of days,but still no clue.here's my senario.I have a main xap where shell.xaml is in.In shell's layout,I wanna dynamic download the other xaps when they are needed.

    ther question is,for example,I pre import a TsTree.xap's Tree.xaml into the List<controls> controls located in shell.xaml.cs,how can I  download the Tsworkspace.xap's Item1workspace.xaml when I click the item1 on Tree.xaml(a treeview only),and show this in the shell.xaml?

    I tried the eventAggregator,but When I try to change the construtor like this

        [Export("Controls",typeof(FrameworkElement))]
        public partial class TreeList : UserControl
        {
             public string SelectedIName { get; set; }
    
            //it's not work
            public TreeList(IEventAggregator eventAggregator)
            {
                InitializeComponent();
                
            }
    
            //It's Ok
            public TreeList()
            {
                InitializeComponent();
    
            }
    
            //the event belong to this control that I wanna
            //use SelectedIName in shell.xaml
            //when SelectedIName is changed SelectedIName.xaml is loaded
            //from another xap.
            //But there's no event in shell.xaml...
            public void TreeList_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
            {
                SelectedIName = ((IDeviceBase)e.NewValue).Name;
                 
            }


    here's my shell.xaml,for now I didn't try to dynamic download xap,because I don't know how to communitate between xap.

        public partial class Shell : UserControl
        {
            #region ローカル 変数
            #region MefShell オブジェクト
            [ImportMany("Controls", AllowRecomposition = true)]
            public List<FrameworkElement> controls = new List<FrameworkElement>();
    
            AggregateCatalog catalog;
            CompositionContainer container;
            #endregion MefShell オブジェクト
    
            List<string> xapfiles = new List<string>();
            string strSelectedXAP = "";
    
            #endregion ローカル 変数
    
            #region Initialize
            public Shell()
            {
                InitializeComponent();
    
                catalog = new AggregateCatalog();
                container = new CompositionContainer(catalog);
                container.ComposeParts(this);
            }
    
            #endregion Initialize
    
            #region Internal Methods
            private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
            {
                catalog.Catalogs.Add(CreateCatalog("TsNavi.xap"));
                catalog.Catalogs.Add(CreateCatalog("TsSiteMap.xap"));
                catalog.Catalogs.Add(CreateCatalog("TsWorkSpace.xap"));
    
                xapfiles.Add("TsNavi.xap");
                xapfiles.Add("TsSiteMap.xap");
            }
    
            #region HelperMethod
    
            private DeploymentCatalog CreateCatalog(string uri)
            {
                var deploymentCatalog = new DeploymentCatalog(uri);
                //logic
                deploymentCatalog.DownloadCompleted += (s, e) =>
                {
                    var ss = (DeploymentCatalog)s;
                    switch (ss.Uri.ToString())
                    {
                        case "TsSiteMap.xap":
                            LoadTsSiteMapXap("TsSiteMapTreeList");
                            break;
                        case "TsNavi.xap":
                            LoadTsNaviXap("TsNaviGuide");
                            break;
                        case "TsWorkSpace.xap":
                            LoadTsWorkSpaceXap("TsWorkSpaceMain");
                            break;
                    }
                };
    
                //削除しないでください
                deploymentCatalog.DownloadProgressChanged += (s, e) =>
                {
    
                };
                deploymentCatalog.DownloadAsync();
                return deploymentCatalog;
            }
    
            
            private void LoadTsWorkSpaceXap(string pageName)
            {
                foreach (var page in this.controls)
                {
                    if (page.Name == pageName)
                    {
                        LayoutRight.Children.Add(page);
                        break;
                    }
                }
            }
    
            private void LoadTsSiteMapXap(string pageName)
            {
                foreach (var page in this.controls)
                {
                    if (page.Name == pageName)
                    {
                        page.SetValue(Grid.RowProperty, 0);
                        LayoutSiteMap.Children.Add(page); 
                        break;
                    }
                }
            }
    
            private void LoadTsNaviXap(string pageName)
            {
                foreach (var page in this.controls)
                {
                    if (page.Name == pageName)
                    {
                        page.SetValue(Grid.ColumnProperty, 1);
                        LayoutModeling.Children.Add(page);
                        break;
                    }
                }
            }
        }
    }


    Wednesday, June 08, 2011 12:34 AM

Answers

  • Hi,
    Create IAggregateCatalog interface then implement with AggregateCatalog.
    When you import IAggregateCatalog type it will return an AggregateCatalog instance.

    Thursday, June 09, 2011 11:24 PM

All replies

  • Hi,

    Two possible:

    1.Event Aggregator = lightweight

    2.Type and Service = heavyweight flexible and useful.

    Wednesday, June 08, 2011 9:33 AM
  • thank you for replying.so,if I wanna choose the eventAggrator,when I change the exported usercontrol's constructor,why it can't be composed by shell? 

    Wednesday, June 08, 2011 8:10 PM
  •     [Export("Controls",typeof(FrameworkElement))]
        public partial class TreeList : UserControl
        {
            //when I delete this,the control can be composed by shell    
            [Import(AllowRecomposition=true)]
            AggregateCatalog _catalog { get; set; }
        }

    I don't know why?Can anybody give me a clue?

    Wednesday, June 08, 2011 8:21 PM
  • Hi,

     

        [Export("Controls",typeof(FrameworkElement))]
        public partial class TreeList : UserControl
        {
            //when I delete this,the control can be composed by shell    
            [Import(AllowRecomposition=true)]
            AggregateCatalog _catalog { get; set; }
        }


    I don't know why?Can anybody give me a clue?

     

    You're wrong you can't Export constructor. use Import attribute so that it will work

     

    [Export("Controls",typeof(FrameworkElement))]
        public partial class TreeList : UserControl
        {
            //when I delete this,the control can be composed by shell   
            [Import(AllowRecomposition=true)]
            IAggregateCatalog _catalog { get; set; } not AggregateCatalog _catalog
        }

    It is a type can't directly use.

    Thursday, June 09, 2011 8:09 AM
  • Hi,

    Use pirsm assemblies.

    Thursday, June 09, 2011 8:18 AM
  • oh,I got it.Thanks!But how should I create the IAggregateCatalog fo r AggregateCatalog?

    Thursday, June 09, 2011 8:35 PM
  • Hi,
    Create IAggregateCatalog interface then implement with AggregateCatalog.
    When you import IAggregateCatalog type it will return an AggregateCatalog instance.

    Thursday, June 09, 2011 11:24 PM
  • ok...so that means I have to create my AggregateCatalog class and IAggregateCatalog?I can't use the System.ComponentModel.Composition.Hosting.AggregateCatalog?

    It sounds too hard for me..

    Friday, June 10, 2011 12:16 AM
  • Take a look at Prism there has excellent MEF sample

    Friday, June 10, 2011 12:20 AM
  • yeah,I am still learning at prism.so,I prefer to use the EventAggregator for now.Thank you for your suggestions!

    Friday, June 10, 2011 12:27 AM