【问题标题】:Modularity in Caliburn microCaliburn micro 中的模块化
【发布时间】:2020-06-01 09:09:11
【问题描述】:

我有主项目(wpf 应用程序)和“子”项目(用户控件类库)。我正在尝试在我的主项目的 MainView 中使用子项目的用户控件:

MainView.xaml:

        <ContentControl  x:Name="ActiveItem" Grid.Column="1">

        </ContentControl>

主视图模型:

        public void LoadAgenda()
        { 
            ActivateItem(new InstanceOfViewModelFromChildProject());
        }

我得到错误

“找不到 InstanceOfViewModelFromChildProject 的视图”。

我认为我收到此错误是因为它试图在我的主项目中而不是在此视图模型和视图所在的子项目中查找视图。有没有办法解决这个问题或其他方法?

【问题讨论】:

    标签: c# wpf caliburn.micro


    【解决方案1】:

    覆盖引导程序的SelectAssemblies() 方法以返回定义视图的程序集。

    来自docs

    默认情况下,基类返回您的应用程序所在的程序集:

    protected override IEnumerable<Assembly> SelectAssemblies()
    {
        return new[] {
            Assembly.GetExecutingAssembly()
        };
    }
    

    【讨论】:

    • @Drasko:你试过这个吗?
    【解决方案2】:

    在继承引导程序类的类中重写此方法

    
            static Func<Type, DependencyObject, object, UIElement> _func;
            protected override void Configure()
            {
                var assembly = System.Reflection.Assembly.Load("YourAssemblyName"); // 
                AssemblySource.Instance.Add(assembly);
    
                _func = ViewLocator.LocateForModelType;
                ViewLocator.LocateForModelType = LocateForModelType;
    
            }
    
            private static Func<Type, DependencyObject, object, UIElement> LocateForModelType = (modelType, displayLocation, context) => {
    
                //use the default method first:
                UIElement view = _func(modelType, displayLocation, context);
                if (!(view is TextBlock))
                    return view;
    
                var viewTypeName = modelType.Name.Replace("Model", string.Empty);
                var viewType = (from assmebly in AssemblySource.Instance
                                from type in assmebly.GetExportedTypes()
                                where type.Name == viewTypeName
                                select type).FirstOrDefault();
    
                return viewType == null ? new TextBlock { Text = string.Format("{0} not found.", viewTypeName) }
                    : Activator.CreateInstance(viewType) as UIElement;
            };
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 2012-01-29
      • 2012-10-24
      • 2012-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多