Visual Studio 2013 Designer崩溃自定义IBehavior

时间:2015-04-27 14:35:16

标签: visual-studio-2013 windows-phone-8.1 win-universal-app

我想使用自定义IBehavior能够在针对Windows Phone 8.1的通用应用中显示/隐藏XAML中的StatusBar

代码:

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:

<Page
    x:Class="test.TestPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:test"
    xmlns:behavior="using:test.Behaviors"
    xmlns:i="using:Microsoft.Xaml.Interactivity"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <i:Interaction.Behaviors>
        <behavior:StatusBarBehavior  Show="False"/>
    </i:Interaction.Behaviors>
    <!-- page content -->
</Page>

已添加Behaviors SDK(12.0)作为项目的参考。

不幸的是,在Visual Studio 2013(社区版,更新4)中,相关页面的设计窗口显示错误:

COMException: Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
StackTrace: Empty
InnerException: None

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

有没有办法解决这个错误?设计窗口非常有必要预览页面的布局......

1 个答案:

答案 0 :(得分:2)

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()在设计视图中抛出错误。