「Avalonia UI - ウインドウ」の版間の差分

ナビゲーションに移動 検索に移動
(ページの作成:「== 概要 == Avalonia UIは、クロスプラットフォームのUIフレームワークであり、C#で開発できるという特徴がある。<br> ウインドウは、Avaloniaアプリケーションの基本的な構成要素の1つである。<br> <br> ウインドウは、ユーザインターフェースの主要なコンテナとして機能して、アプリケーションのメインコンテンツを表示する。<br> 一般的に、<code>Window</cod…」)
 
70行目: 70行目:
|}
|}
</center>
</center>
<br><br>
==  基本的なウインドウ ==
<syntaxhighlight lang="c#">
public class MainWindow : Window
{
    public MainWindow()
    {
      Title = "Avalonia Sample Application";
      Width = 400;
      Height = 300;
      var button = new Button
      {
          Content = "Click Me",
          HorizontalAlignment = HorizontalAlignment.Center,
          VerticalAlignment = VerticalAlignment.Center
      };
      Content = button;
    }
}
</syntaxhighlight>
<br><br>
== カスタムスタイルを適用したウインドウ ==
<syntaxhighlight lang="c#">
public class StyledWindow : Window
{
    public StyledWindow()
    {
      Title                            = "Styled Window";
      Background                        = new SolidColorBrush(Colors.LightBlue);
      TransparencyLevelHint            = WindowTransparencyLevel.AcrylicBlur;
      ExtendClientAreaToDecorationsHint = true;
      ExtendClientAreaChromeHints      = ExtendClientAreaChromeHints.NoChrome;
    }
}
</syntaxhighlight>
<br><br>
== ダイアログウインドウの実装 ==
<syntaxhighlight lang="c#">
public class CustomDialog : Window
{
    public CustomDialog()
    {
      Title = "Confirmation";
      SizeToContent = SizeToContent.WidthAndHeight;
      CanResize = false;
      var stack = new StackPanel();
      stack.Children.Add(new TextBlock { Text = "Are you sure?" });
      var okButton = new Button { Content = "OK" };
      okButton.Click += (s, e) => Close(true);
      var cancelButton = new Button { Content = "Cancel" };
      cancelButton.Click += (s, e) => Close(false);
      var buttonPanel = new StackPanel { Orientation = Orientation.Horizontal };
      buttonPanel.Children.Add(okButton);
      buttonPanel.Children.Add(cancelButton);
      stack.Children.Add(buttonPanel);
      Content = stack;
    }
}
</syntaxhighlight>
<br><br>
== データバインディングを使用したウインドウ ==
<syntaxhighlight lang="c#">
public class DataBoundWindow : Window
{
    public DataBoundWindow()
    {
      Title = "Data Binding Example";
      var viewModel = new MainViewModel();
      DataContext = viewModel;
      var textBox = new TextBox();
      textBox.Bind(TextBox.TextProperty, new Binding("Name"));
      var button = new Button { Content = "Submit" };
      button.Command = ReactiveCommand.Create(viewModel.SubmitCommand);
      var stack = new StackPanel();
      stack.Children.Add(textBox);
      stack.Children.Add(button);
      Content = stack;
    }
}
</syntaxhighlight>
<br><br>
== 複数のウインドウを管理するアプリケーション ==
<syntaxhighlight lang="c#">
public class MainWindow : Window
{
    public MainWindow()
    {
      Title = "Main Window";
      var openNewWindowButton = new Button { Content = "Open New Window" };
      openNewWindowButton.Click += OpenNewWindow;
      Content = openNewWindowButton;
    }
    private void OpenNewWindow(object sender, RoutedEventArgs e)
    {
      var newWindow = new Window
      {
          Title = "New Window",
          Width = 300,
          Height = 200
      };
      newWindow.Show();
    }
}
</syntaxhighlight>
<br><br>
== ウインドウイベントを処理する例 ==
<syntaxhighlight lang="c#">
public class EventHandlingWindow : Window
{
    public EventHandlingWindow()
    {
      Title = "Event Handling Window";
      Opened  += OnWindowOpened;
      Closing += OnWindowClosing;
      Closed  += OnWindowClosed;
    }
    private void OnWindowOpened(object sender, EventArgs e)
    {
      Console.WriteLine("Window opened");
    }
    private void OnWindowClosing(object sender, WindowClosingEventArgs e)
    {
      var result = MessageBox.Show("Are you sure you want to close?", "Confirm", MessageBoxButton.YesNo);
      e.Cancel = (result == MessageBoxResult.No);
    }
    private void OnWindowClosed(object sender, EventArgs e)
    {
      Console.WriteLine("Window closed");
    }
}
</syntaxhighlight>
<br><br>
<br><br>


案内メニュー