sgdesmet
sgdesmet

Reputation: 646

Visual studio 2013 Designer crashing on custom IBehavior

I want to use a custom IBehavior to be able to show/hide the StatusBar from XAML in a Universal app targeted at Windows Phone 8.1.

The code:

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>

The Behaviors SDK (12.0) has been added as a reference to the project.

Unfortunatly in Visual Studio 2013 (Community edition, Update 4), the design window for the page in question shows an error:

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

However, the StatusBarBehavior works perfectly fine when I deploy the app on a device, no errors are thrown.

Is there a way to fix this error? Having the design window is quite necessary to preview the layout of the pages...

Upvotes: 0

Views: 134

Answers (1)

sgdesmet
sgdesmet

Reputation: 646

Changing the OnVisibleChanged event to

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();
        }

fixes the issue. I assume StatusBar.GetForCurrentView() throws an error in the design view.

Upvotes: 2

Related Questions