无法在设计器中更新WPF用户控件

时间:2020-02-22 02:51:40

标签: c# wpf

我真的迷失了方向,我做了网上发现的一切,但仍然没有机会。

我有一个简单的矩形和一个红色/蓝色枚举。我想通过更改用户的值在设计器中设置其颜色的动画。

MyCustomUC.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.ComponentModel;
using System.Diagnostics;

namespace WpfApp1
{
    public enum Pill
    { red = 1, blue = 2 }

    public partial class MyCustomUC : UserControl
    {
        private Color ColorToAnim = Color.FromRgb(255, 0, 0);

        public static DependencyProperty pill_mode = DependencyProperty.Register(
        "pillmodeval", typeof(Pill), typeof(MyCustomUC),
         new PropertyMetadata(Pill.blue, // ISSUE 1 : Default Value Is Not Working , It's always 0
         new PropertyChangedCallback(OnValueChanged)
        )); 

        [Category("Pills")]
        public Pill pillmodeval
        {
            get { return (Pill)GetValue(pill_mode); }
            set { SetValue(pill_mode, value); }
        }

        public MyCustomUC()
        {
            InitializeComponent(); //// Not Firing Either 
            color_output.Fill = new SolidColorBrush(ColorToAnim);

            // ISSUE 2 : MessageBox.Show , Debug.WriteLine Not Work On Design Mode
            MessageBox.Show("MyCustomUC Created!");
            Debug.WriteLine("MyCustomUC Created!");
        }

        void AnimateColor()
        {
            ColorAnimation new_color_anim = new ColorAnimation(ColorToAnim, TimeSpan.FromSeconds(1));
            color_output.Fill.BeginAnimation(SolidColorBrush.ColorProperty, new_color_anim);
        }

        void UpdateColor()
        {
            if (pillmodeval == Pill.red)
            {
                ColorToAnim = Color.FromRgb(0, 0, 255);
                pillmodeval = Pill.blue;
            }
            else
            {
                ColorToAnim = Color.FromRgb(255, 0, 0);
                pillmodeval = Pill.red;
            }
        }

        private void color_output_MouseUp(object sender, MouseButtonEventArgs e)
        {
            UpdateColor();
        }

        private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            MyCustomUC custom_uc = (MyCustomUC)d;
            // ISSUE 3 : AnimateColor not working in Designer
            custom_uc.AnimateColor();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            // ISSUE 2 : MessageBox.Show , Debug.WriteLine Not Work On Design Mode
            MessageBox.Show("MyCustomUC Loaded!");
            Debug.WriteLine("MyCustomUC Loaded!");
        }
    }
}

MyCustomUC.xaml:

<UserControl x:Class="WpfApp1.MyCustomUC"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WpfApp1"
             mc:Ignorable="d" 
             d:DesignHeight="100" d:DesignWidth="100" Loaded="UserControl_Loaded">
    <Grid>
            <Rectangle x:Name="color_output" Fill="Black" MouseUp="color_output_MouseUp" />
    </Grid>
</UserControl>

问题:

  • 构造函数调用在Designer中不起作用
  • OnLoad调用在Designer中不起作用
  • AnimateColor在Designer中不起作用
  • DependencyProperty的默认值始终为零,不适用于枚举。

如何使此代码起作用?

1 个答案:

答案 0 :(得分:0)

这就是Visual Studio XAML设计器的工作方式。您看不到动画,因为您将库称为解决方案项目。 VS设计器不会在设计时为引用为解决方案项目的库运行任何编译时代码。

User control referenced as Project

VS设计器将仅向您显示无需编译整个库即可应用的更改。为此,您不必使用背后的代码,而必须使用XAML绑定,例如:

<UserControl 
    x:Class="MyUserControlLibrary.UserControl1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:local="clr-namespace:MyUserControlLibrary"
    mc:Ignorable="d" >
    
    <UserControl.Resources>
        <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
    </UserControl.Resources>

    <StackPanel  DataContext="{Binding Mode=Default, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}">
        <TextBlock  Visibility="{Binding ShowIt, Converter={StaticResource BooleanToVisibilityConverter}}">Abc</TextBlock>
    </StackPanel>
    
</UserControl>

public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
    }

    public bool ShowIt
    {
        get { return (bool)GetValue(ShowItProperty); }
        set { SetValue(ShowItProperty, value); }
    }
    public static readonly DependencyProperty ShowItProperty =
        DependencyProperty.Register("ShowIt", typeof(bool), typeof(UserControl1), new PropertyMetadata(true));

}

您可以从materialdesigninxaml.net库中的控件中看到动画,因为您直接将其作为DLL库引用。如果您将库引用为已编译的DLL,则动画应能按预期工作。

User control referenced as DLL

相关问题