【问题标题】:Splash screen not working in wpf using mvvm使用 mvvm 在 wpf 中启动画面不起作用
【发布时间】:2018-05-09 05:50:39
【问题描述】:

我正在尝试显示启动画面(即)我为此分别创建了启动视图和视图模型。我正在使用 mvvm 订阅者和发布者管理其可见性。

但我目前面临的问题是,当任何任务开始时,我的启动画面都不可见。

启动画面视图:

<Grid>
        <!--Splash Screen -->
        <Border x:Name="Splashbrd"  Style="{StaticResource SettingOuterBorderStyle}" 
                        Visibility="{Binding EnableSplash,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" >
            <Border Style="{StaticResource ProgressBorderStyle}">
                <StackPanel  Margin="10"  >
                    <ProgressBar Style="{StaticResource ProgressBarStyle}"/>
                    <TextBlock Text="{Binding SplashText,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"  FontFamily="Arial" FontSize="12"
                                        Margin="5,5,5,0" TextAlignment="Center" TextWrapping="Wrap" />
                </StackPanel>
            </Border>
        </Border>
    </Grid>

SplashScreenViewModel

private void SubscribeSplashScreenEvent()
        {
            SubscriptionToken _subscribeSplashEvent = this._eventAggregator.GetEvent<PubSubEvent<ISplashScreenEvent>>().Subscribe(i =>
            {
                Showprogress(i.SplashVisibilty);
                SplashText = i.SplashText;
            });
        }


        #region SplashScreen
        private Visibility _enablesplash = Visibility.Hidden;
        public Visibility EnableSplash
        {
            get { return _enablesplash; }
            set
            {
                if (_enablesplash != value)
                {
                    _enablesplash = value;
                    RaisePropertyChanged();
                }
            }
        }

        private string _splashtext;
        public string SplashText
        {
            get { return _splashtext; }
            set
            {
                _splashtext = value;
                RaisePropertyChanged();
            }
        }
        public void Showprogress(bool bvisible)
        {
            EnableSplash = bvisible ? Visibility.Visible : Visibility.Hidden;
        }
        #endregion

ViewWhereIamUsing

private void PublishSplashScreenEvent(bool _splashVisibilty, string _splashText = null)
        {
            var eventObj = new SplashScreenEvent
            {
                SplashVisibilty = _splashVisibilty,
                SplashText = _splashText
            };
            eventAggregator.GetEvent<PubSubEvent<ISplashScreenEvent>>().Publish(eventObj);
        }
 BackgroundWorker impworker = new BackgroundWorker();
                impworker.DoWork += test;
                impworker.RunWorkerCompleted += OntestCompleted;

//void test()
{
 PublishSplashScreenEvent(true, "testimg spash......");
}
void OntestCompleted()
{
PublishSplashScreenEvent(false);
}

如何注册

<ContentControl prism:RegionManager.RegionName="{x:Static inf:RegionNames.SplashScreenView}"></ContentControl>

_innerRegionManager.RegisterViewWithRegion(RegionNames.SplashScreenView, typeof(SplashScreenView));

请让我知道我错在哪里以及为什么我的启动画面无法正常工作。

【问题讨论】:

    标签: c# wpf xaml mvvm subscriber


    【解决方案1】:

    我查看了代码,我认为问题可能出在 RaisePropertyChanged 调用上。

    尝试提供属性名称:

    RaisePropertyChanged("EnableSplash")
    

    另外,我认为您不需要在 Visibility 属性中强制 Mode=TwoWay。

    【讨论】:

    • 尝试了您的更改仍然无法正常工作..一个问题是在我目前正在做的后台工作人员中使用事件发布是个好主意..不确定
    • 您是否确保视图与正确的 DataContext 链接?我在 xaml 上看不到那部分。关于您的问题;没问题,但请确保在 UI 线程上进行 UI 更新(如果需要,更改上下文)。
    猜你喜欢
    • 2018-03-22
    • 1970-01-01
    • 2014-06-27
    • 1970-01-01
    • 2017-06-15
    • 1970-01-01
    • 2012-08-06
    • 2021-05-12
    相关资源
    最近更新 更多