caliburn

sz L·2023년 4월 24일
0

WPF

목록 보기
5/9
post-thumbnail
post-custom-banner

  • 솔루션 nuget 패키지 관리

App.xaml

<Application x:Class="wp09_caliburnApp.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:wp09_caliburnApp">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary>
                    <local:Bootstrapper x:Key="Bootstrapper"/>
                 </ResourceDictionary>
                
                <ResourceDictionary>
                    <ResourceDictionary.MergedDictionaries>
                        <!-- MahApps.Metro resource dictionaries. Make sure that all file names are Case Sensitive! -->
                        <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
                        <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
                        <!-- Theme setting -->
                        <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Themes/Light.Olive.xaml" />
                    </ResourceDictionary.MergedDictionaries>
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
         
    </Application.Resources>
</Application>

Bootstrapper.cs

using Caliburn.Micro;   // BootstrapperBase 사용하기위함
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using wp09_caliburnApp.ViewModels;      // MainViewModel클래스 쓰기 위함

namespace wp09_caliburnApp
{
    // Caliburn 으로 MVVM 실행할 때 주요설정 진행
    public class Bootstrapper : BootstrapperBase
    {
        public Bootstrapper()
        {
            Initialize(); // Caliburn MVVM 초기화
        }

        // 시작한 후에 초기화 진행
        protected async override void OnStartup(object sender, StartupEventArgs e)
        {
            //base.OnStartup(sender, e);  // 부모 클래스 실행은 주석처리
            await DisplayRootViewForAsync<MainViewModel>();
        }
    }
}

Views

MainView.xaml

<mah:MetroWindow x:Class="wp09_caliburnApp.Views.MainView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"
    xmlns:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks"
    xmlns:local="clr-namespace:wp09_caliburnApp.Views"
    mc:Ignorable="d"
    Title="MainView" Height="320" Width="480" WindowStartupLocation="CenterScreen">
    <Grid Margin="15">
        <Grid.RowDefinitions>
            <RowDefinition Height="32"/>
            <RowDefinition Height="32"/>
            <RowDefinition Height="32"/>
            <RowDefinition Height="32"/>
            <RowDefinition Height="32"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="80"/>
            <ColumnDefinition Width="100"/>
            <ColumnDefinition Width="80"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>

        
        <Label Grid.Row="0" Grid.Column="0" Content="이름"/>
        <!--Source-->
        <TextBox x:Name="FirstName" Grid.Row="0" Grid.Column="1" Margin="2"/>
        <!--target-->
        <Label Grid.Row="0" Grid.Column="2" Content="{Binding FirstName}" />  
        <!--<TextBox Grid.Row="0" Grid.Column="0" Text="{Binding Path=FirstName, Mode=TwoWay}" />--> <!--위와 같은 실행결과--> 

        <Label Grid.Row="1" Grid.Column="0" Content=""/>
        <TextBox x:Name="LastName" Grid.Row="1" Grid.Column="1" Margin="2"/>
        <Label Grid.Row="1" Grid.Column="2" Content="{Binding LastName}" />

        <Label Grid.Row="2" Grid.Column="0" Content="전체 이름"/>
        <Label Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2" Content="{Binding FullName}" Margin="2"/>

        <Label Grid.Row="3" Grid.Column="0" Content="매니저"/>
        <ComboBox x:Name="Managers" SelectedItem ="{Binding Path=SelectedManager}" DisplayMemberPath="LastName"
                  Grid.Row="3" Grid.Column="1" Margin="2"/>

        <Button x:Name="ClearName" Grid.Row="4" Grid.Column="1" Content="초기화" Margin="2" 
                Style="{StaticResource MahApps.Styles.Button.MetroSquare.Accent}"/>

    </Grid>
</mah:MetroWindow>

MainView.xaml.cs

using MahApps.Metro.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace wp09_caliburnApp.Views
{
    /// <summary>
    /// MainWindow.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class MainView : MetroWindow
    {
        public MainView()
        {
            InitializeComponent();
        }
    }
}

ViewModels

MainViewModel.cs

using Caliburn.Micro;
using System;
using System.Collections.Generic;
using System.IO.Pipes;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using wp09_caliburnApp.Models;

namespace wp09_caliburnApp.ViewModels
{
    public class MainViewModel : Screen
    {
        //Caliburn 버전업 되면서 사용방식 바뀐것
        private string firstName = "SuJin";
        public string FirstName 
        {
            get => firstName; 
            set
            {
                firstName = value;
                NotifyOfPropertyChange(() => FirstName);        // 속성값이 변경된걸 시스템에 알려주는 역할
                NotifyOfPropertyChange(nameof(CanClearName));
                NotifyOfPropertyChange(nameof(FullName));
            }
        }
        private string lastName = "Lee";
        public string LastName
        {
            get => lastName;
            set
            {
                lastName = value;
                NotifyOfPropertyChange(() => LastName);
                NotifyOfPropertyChange(() => FullName); //변화 통보
            }
        }
        public string FullName
        {
            get => $"{LastName} {FirstName}";
        }

        // 콤보박스에 바인딩 할 속성
        // 이런곳에선 var를 쓸 수 없음
        private BindableCollection<Person> managers = new BindableCollection<Person>();

        public BindableCollection<Person> Managers
        {
            get => managers;
            set => managers = value;
        }

        // 콤보박스에서 선택된 값을 지정할 속성
        private Person selectedManager;
        public Person SelectedManager
        {
            get => selectedManager;
            set
            {
                selectedManager = value;
                LastName = selectedManager.LastName;
                FirstName = selectedManager.FirstName;
                NotifyOfPropertyChange(nameof(SelectedManager));
            }
        }
        public MainViewModel()
        {
            // DB를 사용하면 여기서 DB 접속 => 데이터 Select까지
            Managers.Add(new Person { FirstName = "Tim", LastName = "Carmack" });
            Managers.Add(new Person { FirstName = "Steve", LastName = "Jobs" });
            Managers.Add(new Person { FirstName = "Bill", LastName = "Gates" });
            Managers.Add(new Person { FirstName = "Elon", LastName = "Musk" });
        }

        // 버튼 이벤트
        public void ClearName()
        {
            FirstName = string.Empty;
            LastName = string.Empty;
        }

        //메서드와 이름 동일하게 하고 앞에 Can을 붙임
        public bool CanClearName
        {
            get => !string.IsNullOrEmpty(firstName) && !string.IsNullOrEmpty(lastName);
        }
    }
}

Models

Person.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace wp09_caliburnApp.Models
{
    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
}

실행결과

profile
가랑비는 맞는다 하지만 폭풍은 내 것이야
post-custom-banner

0개의 댓글