定制caliburn.micro防溅屏幕,带外壳屏蔽导体 [英] Custom caliburn.micro splashscreen with shell screen conductor

查看:99
本文介绍了定制caliburn.micro防溅屏幕,带外壳屏蔽导体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是WPF和Caliburn.micro的新手,我想使用Caliburn为WPF应用实现自定义启动屏幕。
我正在寻找一种使用屏蔽导体的正确方法(据我了解,这是最佳解决方案)。



我的Bootstrapper看起来像这样:

 公共类AppBootstrapper:BootstrapperBase 
{
实际上是布尔值;
个私人CompositionContainer容器;

公共AppBootstrapper()
{
Start();
}

受保护的覆盖无效Configure()
{
容器= new CompositionContainer(
new AggregateCatalog(AssemblySource.Instance.Select(x => new AssemblyCatalog(x))。OfType< ComposablePartCatalog>())
);

var batch = new compositionBatch();

批处理。AddExportedValue< IWindowManager>(new WindowManager());
batch.AddExportedValue< IEventAggregator>(new EventAggregator());
batch.AddExportedValue(container);
container.Compose(batch);
Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = new CultureInfo( en-US);
}

受保护的覆盖对象GetInstance(类型serviceType,字符串键)
{
string contract = string.IsNullOrEmpty(key)? AttributedModelServices.GetContractName(serviceType):键;
var出口=容器.GetExportedValues< object>(合同);

如果(exports.Any())返回export.First();
throw new Exception(string.Format(找不到合同{0}的任何实例。,合同));
}

受保护的覆盖IEnumerable< object> GetAllInstances(Type serviceType)
{
返回容器。GetExportedValues< object>(AttributedModelServices.GetContractName(serviceType));;
}

受保护的重写void BuildUp(对象实例)
{
container.SatisfyImportsOnce(instance);
}

受保护的重写void OnStartup(对象发送者,StartupEventArgs e)
{
DisplayRootViewFor< ShellViewModel>();
}
}

ShellViewModel就像这样:(但我不实际上想看到它的窗口)

  [Export(typeof(ShellViewModel))] 
公共类ShellViewModel:Conductor< ;屏幕>
{
[ImportingConstructor]
public ShellViewModel()
{
ActivateItem(new SplashScreenViewModel());
int i = 10; //正在加载...或在初始屏幕内加载?
ActivateItem(new MainWindowViewModel());
}
}

SplashScreenViewModel非常简单:

  [Export(typeof(SplashScreenViewModel))] 
公共类SplashScreenViewModel:屏幕
{
私有字符串appName;
私有字符串版本;
私人字串服务;
私有字符串创建者;
私有字符串版权;
私人字串讯息;

[ImportingConstructor]
public SplashScreenViewModel()
{
appName = Assembly.GetEntryAssembly()。GetName()。Name;
版本= Assembly.GetEntryAssembly()。GetName()。Version.ToString();
copyright = Copyright©corp 2014;
service =部门;
creator = user- +服务;
message =正在加载...;
}
}

最后,MainWindowViewModel:

  [Export(typeof(MainWindowViewModel))] 
公共类MainWindowViewModel:屏幕,IGuardClose
{
[ImportingConstructor]
public MainWindowViewModel()
{
NetworkUpdate(); // 做东西。
}
void IGuardClose.CanClose(Action< bool>回调)
{
throw new NotImplementedException();
}

void IClose.TryClose()
{
throw new NotImplementedException();
}
}

现在我正在尝试每种方法但是它什么也不显示,或者仅显示主窗口,或者仅显示初始屏幕,甚至对于shellview来说都是空白视图。



对此,我真的很感谢! p>

谢谢大家……

解决方案

不要将ActivateItem用于启动屏幕。



根据我对飞溅的了解,最好使用WindowManager(在exe加载所有必要进程时显示的弹出窗口)



您可以在Appbootstrapper的OnStartUp中完成

 受保护的覆盖无效OnStartup(对象发送者,StartupEventArgs e )
{
var splash = this.container.GetExportedValue< SplashScreenViewModel>();
var windowManager = IoC.Get< IWindowManager>();
windowManager.ShowDialog(splash);

//使用
在这里进行后台工作var bw = new BackgroundWorker();
bw.DoWork + =(s,e)=>
{
//在这里进行后台处理

};

bw.RunWorkerCompleted + =(s,e)=>
{
//关闭启动画面
splash.TryClose();
this.DisplayRootViewFor< ShellViewModel>();
};

bw.RunWorkerAsync();
}


//您的ShellViewModel
[ImportingConstructor]
public ShellViewModel(MainViewModel mainViewModel)
{
this.DisplayName =窗口标题;
//无需新建
this.ActivateItem(mainViewModel);
}


I'm new to WPF and Caliburn.micro and I'd like to implement a custom splashscreen to a WPF App using Caliburn. I'm looking at the right way to do it with a screen conductor (as I understood it is the best solution).

My Bootstrapper looks like this :

public class AppBootstrapper : BootstrapperBase
    {
        private bool actuallyClosing;
        private CompositionContainer container;        

        public AppBootstrapper()
        {
            Start();
        }

        protected override void Configure()
        {
            container = new CompositionContainer(
                    new AggregateCatalog(AssemblySource.Instance.Select(x => new AssemblyCatalog(x)).OfType<ComposablePartCatalog>())
                );

            var batch = new CompositionBatch();

            batch.AddExportedValue<IWindowManager>(new WindowManager());
            batch.AddExportedValue<IEventAggregator>(new EventAggregator());
            batch.AddExportedValue(container);
            container.Compose(batch);
            Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
        }

        protected override object GetInstance(Type serviceType, string key)
        {
            string contract = string.IsNullOrEmpty(key) ? AttributedModelServices.GetContractName(serviceType) : key;
            var exports = container.GetExportedValues<object>(contract);

            if (exports.Any()) return exports.First();
            throw new Exception(string.Format("Could not locate any instances of contract {0}.", contract));
        }

        protected override IEnumerable<object> GetAllInstances(Type serviceType)
        {
            return container.GetExportedValues<object>(AttributedModelServices.GetContractName(serviceType));
        }

        protected override void BuildUp(object instance)
        {
            container.SatisfyImportsOnce(instance);
        }

        protected override void OnStartup(object sender, StartupEventArgs e)
        {
            DisplayRootViewFor<ShellViewModel>();
        }
    }

ShellViewModel is like this : (but I don't actually wanna see a window of it)

[Export(typeof(ShellViewModel))]
public class ShellViewModel : Conductor<Screen>
{
    [ImportingConstructor]
    public ShellViewModel()
    {
         ActivateItem(new SplashScreenViewModel());
         int i = 10; // loading ... or do the loading inside the splashscreen ??
         ActivateItem(new MainWindowViewModel());
    }
}

SplashScreenViewModel is quite simple :

[Export(typeof(SplashScreenViewModel))]
    public class SplashScreenViewModel : Screen
    {
        private string appName;
        private string version;
        private string service;
        private string creator;
        private string copyright;
        private string message;

        [ImportingConstructor]
        public SplashScreenViewModel()
        {
            appName = Assembly.GetEntryAssembly().GetName().Name;
            version = Assembly.GetEntryAssembly().GetName().Version.ToString();
            copyright = "Copyright © corp 2014";
            service = "Department";
            creator = "user - " + Service;
            message = "Loading ...";
        }
    }

And finally, MainWindowViewModel :

[Export(typeof(MainWindowViewModel))]
public class MainWindowViewModel : Screen, IGuardClose
{
    [ImportingConstructor]
    public MainWindowViewModel()
    {
        NetworkUpdate(); // do stuff.
    }
    void IGuardClose.CanClose(Action<bool> callback)
    {
        throw new NotImplementedException();
    }

    void IClose.TryClose()
    {
        throw new NotImplementedException();
    }
}

Right now I'm trying a little bit every approach but it displays either nothing or the main window only or the splashscreen only or even an empty view for shellview ...

I really would appreciate some hints on this !

Thanks guys ...

解决方案

Dont use ActivateItem for splash screen.

It is better to use WindowManager, based on what I understand about splash ( pop up window displayed while the exe load all the necessary processes)

you can do it in Appbootstrapper in OnStartUp

protected override void OnStartup(object sender, StartupEventArgs e)
    {
        var splash = this.container.GetExportedValue<SplashScreenViewModel>();
        var windowManager = IoC.Get<IWindowManager>();
        windowManager.ShowDialog(splash);

       // do your background work here using
        var bw = new BackgroundWorker();
        bw.DoWork += (s, e) =>
            {
                // Do your background process here

            };

        bw.RunWorkerCompleted += (s, e) =>
            {
                // close the splash window
                splash.TryClose();
                this.DisplayRootViewFor<ShellViewModel>();
            }; 

        bw.RunWorkerAsync();           
    }


    // your ShellViewModel
    [ImportingConstructor]
    public ShellViewModel(MainViewModel mainViewModel)
    {
        this.DisplayName = "Window Title";
        // no need to new
        this.ActivateItem(mainViewModel);
    }

这篇关于定制caliburn.micro防溅屏幕,带外壳屏蔽导体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆