Visual studio 2013 Visual studio 2013 Designer在自定义IBehavior上崩溃

Visual studio 2013 Visual studio 2013 Designer在自定义IBehavior上崩溃,visual-studio-2013,windows-phone-8.1,win-universal-app,Visual Studio 2013,Windows Phone 8.1,Win Universal App,我想在针对Windows Phone 8.1的通用应用程序中使用自定义的IBehavior来显示/隐藏XAML中的状态栏 守则: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Xaml.Interactivity; using Windows.ApplicationModel; u

我想在针对Windows Phone 8.1的通用应用程序中使用自定义的
IBehavior
来显示/隐藏XAML中的
状态栏

守则:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xaml.Interactivity;
using Windows.ApplicationModel;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;

namespace test.Behaviors
{
    public class StatusBarBehavior : DependencyObject, IBehavior
    {

        public DependencyObject AssociatedObject { get; private set; }

        public void Attach(DependencyObject associatedObject) { }

        public void Detach() { }

        public StatusBarBehavior()
        {

        }

        public bool Show
        {
            get { return Convert.ToBoolean(GetValue(ShowProperty)); }
            set { SetValue(ShowProperty, value); }
        }

        public static readonly DependencyProperty ShowProperty =
            DependencyProperty.Register("Show",
            typeof(object),
            typeof(StatusBarBehavior),
            new PropertyMetadata(null, OnIsVisibleChanged));

        private static async void OnIsVisibleChanged(DependencyObject d,
            DependencyPropertyChangedEventArgs e)
        {
            var statusBar = StatusBar.GetForCurrentView();
            if (statusBar == null || e == null || e.NewValue == null || DesignMode.DesignModeEnabled)
                return;

            if (Convert.ToBoolean(e.NewValue))
                await statusBar.ShowAsync();
            else
                await statusBar.HideAsync();
        }
    }
}
XAML:

但是,当我在设备上部署应用程序时,
StatusBarBehavior
工作得非常好,不会引发任何错误


有没有办法修复此错误?拥有设计窗口对于预览页面布局非常必要…

OnVisibleChanged
事件更改为

private static async void OnIsVisibleChanged(DependencyObject d,
            DependencyPropertyChangedEventArgs e)
        {
            if (DesignMode.DesignModeEnabled) { return; }

            var statusBar = StatusBar.GetForCurrentView();
            if (statusBar == null || e == null || e.NewValue == null)
                return;

            if (Convert.ToBoolean(e.NewValue))
                await statusBar.ShowAsync();
            else
                await statusBar.HideAsync();
        }

解决了这个问题。我假设
StatusBar.GetForCurrentView()
在设计视图中抛出了一个错误。

哇,太感谢你了,这已经困扰我太久了
COMException: Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
StackTrace: Empty
InnerException: None
private static async void OnIsVisibleChanged(DependencyObject d,
            DependencyPropertyChangedEventArgs e)
        {
            if (DesignMode.DesignModeEnabled) { return; }

            var statusBar = StatusBar.GetForCurrentView();
            if (statusBar == null || e == null || e.NewValue == null)
                return;

            if (Convert.ToBoolean(e.NewValue))
                await statusBar.ShowAsync();
            else
                await statusBar.HideAsync();
        }