10

Windows フォームを使用すると、デザイナーを持つことができる非ビジュアル要素であるコンポーネントを開発できます。組み込みコンポーネントには、BackgroundWorker、Timer、および多くの ADO .NET オブジェクトが含まれます。これは、複雑なオブジェクトを簡単に構成できる優れた方法であり、デザイナーによるデータ バインディングを可能にします。

私はWPFを見てきましたが、コンポーネントの概念がないようです。私はこれについて正しいですか?私が見逃したコンポーネント (またはコンポーネントのようなもの) を作成する方法はありますか?

多くの調査の結果、ファンシーなアドナーがおそらくこれを行う唯一の方法であると感じたため、ボブの回答を受け入れました。

4

4 に答える 4

6

私自身の観察から、MicrosoftはGUIにコンポーネントや同様のものを持たないようにしようとしているようです。WPFは、XAMLにあるもののほとんどを厳密にGUIのものに制限しようとしていると思います。データバインディングが唯一の例外だと思います。私は、他のほとんどすべてをコードビハインドまたは個別のクラスまたはアセンブリに保持しようとしていることを知っています。

おそらくあなたが望んでいた答えとは正確には一致しませんが、それは私の$0.02です。

于 2008-10-01T16:44:33.020 に答える
2

同じ質問があります。コンポーネントのようなメカニズムの利点は、デザイナーがそれを Blend に追加し、デザイナーでプロパティ エディターを使用して構成し、データ バインディングを使用できることです。以下の解決策についてどう思いますか?できます。

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>
于 2010-09-29T16:31:25.610 に答える
1

リソース ディクショナリ内には、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>
于 2009-02-03T13:59:01.930 に答える
1

これまでのところ、理にかなっている唯一のアプローチは、クラスのインスタンスを静的リソースにして、XAML から構成することです。これは機能しますが、WinForms デザイナ コンポーネント トレイのようなものがあればいいと思います。

于 2008-10-01T14:13:24.457 に答える