Windows窗体允许您开发可以拥有设计器的组件,非可视元素。内置组件包括BackgroundWorker,Timer和许多ADO .NET对象。这是一种提供简单配置复杂对象的好方法,它可以实现设计人员辅助的数据绑定。

我一直在关注WPF,看起来似乎没有任何组件概念。我这是对的吗?是否有一些创建我错过的组件(或类似组件)的方法?

我接受了鲍勃的回答,因为经过大量的研究后,我觉得花哨的装扮师可能是唯一的方法。

有帮助吗?

解决方案

仅仅从我自己的观察来看,似乎微软正试图摆脱GUI中的组件和类似的东西。我认为WPF试图将XAML中的大部分内容限制为严格的GUI内容。数据绑定我猜是唯一的例外。我知道我会尝试将大部分内容保存在代码隐藏或单独的类或程序集中。

可能不是你想要的答案,但这是我的0.02美元。

其他提示

我有同样的问题。类似组件的机制的优点是设计人员可以在Blend中添加它,使用Properties编辑器在设计器中配置它,并使用数据绑定。您如何看待下面的解决方案?它有效。

public class TimerComponent : FrameworkElement
{
    public Timer Timer { get; protected set; }

    public TimerComponent()
    {
        if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
        {
            Visibility = Visibility.Collapsed;
            Timer = new Timer(OnTimerTick, null, Timeout.Infinite, Timeout.Infinite);
        }
    }

    void OnTimerTick(object ignore)
    {
        Dispatcher.BeginInvoke(new Action(RaiseTickEvent));
    }

    #region DueTime Dependency Property

    public int DueTime
    {
        get { return (int)GetValue(DueTimeProperty); }
        set { SetValue(DueTimeProperty, value); }
    }

    public static readonly DependencyProperty DueTimeProperty =
        DependencyProperty.Register("DueTime", typeof(int), typeof(TimerComponent), new UIPropertyMetadata(new PropertyChangedCallback(OnDueTimeChanged)));

    static void OnDueTimeChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        var target = obj as TimerComponent;
        if (target.Timer != null)
        {
            var newDueTime = (int)e.NewValue;
            target.Timer.Change(newDueTime, target.Period);
        }
    }

    #endregion

    #region Period Dependency Property

    public int Period
    {
        get { return (int)GetValue(PeriodProperty); }
        set { SetValue(PeriodProperty, value); }
    }

    public static readonly DependencyProperty PeriodProperty =
        DependencyProperty.Register("Period", typeof(int), typeof(TimerComponent), new UIPropertyMetadata(new PropertyChangedCallback(OnPeriodChanged)));

    static void OnPeriodChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        var target = obj as TimerComponent;
        if (target.Timer != null)
        {
            var newPeriod = (int)e.NewValue;
            target.Timer.Change(target.DueTime, newPeriod);
        }
    }

    #endregion

    #region Tick Routed Event

    public static readonly RoutedEvent TickEvent = EventManager.RegisterRoutedEvent(
        "Tick", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(TimerComponent));

    public event RoutedEventHandler Tick
    {
        add { AddHandler(TickEvent, value); }
        remove { RemoveHandler(TickEvent, value); }
    }

    private void RaiseTickEvent()
    {
        RoutedEventArgs newEventArgs = new RoutedEventArgs(TimerComponent.TickEvent);
        RaiseEvent(newEventArgs);
    }

    #endregion
}

使用如下。

<StackPanel>
    <lib:TimerComponent Period="{Binding ElementName=textBox1, Path=Text}" Tick="OnTimerTick" />
    <TextBox x:Name="textBox1" Text="1000" />
    <Label x:Name="label1" />
</StackPanel>

到目前为止,我认为唯一有意义的方法是将类的实例设置为静态资源并从XAML配置它。这可行,但如果有类似WinForms设计器组件托盘的东西可以存在,那就太好了。

你可以在资源词典中放入你喜欢的任何内容,包括与Wpf无关的类。

以下XAML添加字符串“Hello”直接进入窗口(实际字符串,而不是显示字符串的控件),您可以使用相同的方法放置任何内容 - 包括您自己编写的类到XAML文件中。

<Window  x:Class="MyApp.Window1"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    >
<Window.Resources>
    <sys:String x:Key="MyString">Hello</sys:String>
</Window.Resources>
</Window>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top