하아찡

[C#/WPF] Upbit프로젝트 DialogAccess 본문

C#/코인프로그램 - 코드

[C#/WPF] Upbit프로젝트 DialogAccess

하아찡 2023. 11. 27. 16:21

 

 

DialogAccess.xaml

<UserControl x:Class="Upbit.Views.DialogAccess"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:prism="http://prismlibrary.com/"          
             xmlns:conv ="clr-namespace:Upbit.Converter"
             xmlns:tp="clr-namespace:TextBoxPlaceHolder;assembly=TextBoxPlaceHolder"
             MinWidth="600"
             prism:ViewModelLocator.AutoWireViewModel="True">
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/TextBoxplaceHolder;component/RTextBoxPlaceHolder.xaml"/>
            </ResourceDictionary.MergedDictionaries>

            <Style TargetType="StackPanel">
                <Setter Property="Margin" Value="0,0,0,10" />
                <Setter Property="Orientation" Value="Horizontal"/>
            </Style>
        </ResourceDictionary>
        
    </UserControl.Resources>

    <Grid x:Name="MainGrid" Margin="5" >
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*"/>
            <ColumnDefinition Width="1*"/>
        </Grid.ColumnDefinitions>
        <Grid Grid.Row="0" Grid.Column="0">
            <tp:CTextBoxPlaceHolder  Style="{StaticResource ResourceKey=TextBoxPlaceHolder}" x:Name="TextBox" HorizontalAlignment="Stretch" Height="50" Width="auto" Margin="2" TextValue="{Binding AccessKey,Mode=TwoWay}" VerticalAlignment="Top"  PlaceHolder="AccessKey"/>
        </Grid>
        <Grid Grid.Row="0" Grid.Column="1">
            <tp:CTextBoxPlaceHolder  Style="{StaticResource ResourceKey=TextBoxPlaceHolder}" x:Name="CTextBox" HorizontalAlignment="Stretch" Height="50"  Margin="2" TextValue="{Binding SecretKey,Mode=TwoWay}" VerticalAlignment="Top"  PlaceHolder="SecretKey"/>
        </Grid>
        <Button Command="{Binding CloseDialogCommand}" CommandParameter="true" Content="{Binding Lang.LYes}" Width="75" Height="25" HorizontalAlignment="Right" Margin="0,10,0,0" Grid.Row="1" Grid.Column="1" IsDefault="True" />
    </Grid>
</UserControl>

 

외부프로젝트에 있는 리소스사전을 불러다 사용할라면 아래코드와같이 등록을해서 사용해야합니다.

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="pack://application:,,,/TextBoxplaceHolder;component/RTextBoxPlaceHolder.xaml"/>
</ResourceDictionary.MergedDictionaries>

(프로젝트명)  해당리소스가 만들어진 프로젝트명으로 변경해서 사용하면됩니다.

pack://application:,,,/(프로젝트명);component/RTextBoxPlaceHolder.xaml

 

CTextBoxPlaceHolder

Xaml작업해둔 글이 있어서 링크남겨드리겠습니다.

https://thesh.tistory.com/26

 

XAML TextBox에 TextBlock을 사용하여 PlaceHolder를 추가함. - 1

일단 기본적인 개념은 아래 사이트에서 공부해보시면 생각보다 좋은내용들이 있습니다. https://www.tutorialspoint.com/xaml/index.htm XAML Tutorial XAML Tutorial - Welcome to the XAML tutorial for beginners. This tutorial puts

thesh.tistory.com

 

꼭꼭꼭 스타일 추가해두세요... 다 작업해놓고 왜 안뜨지 계속 쳐다만보고있었네요...머쓱

<tp:CTextBoxPlaceHolder  Style="{StaticResource ResourceKey=TextBoxPlaceHolder}" x:Name="TextBox" HorizontalAlignment="Stretch" Height="50" Width="auto" Margin="2" TextValue="{Binding AccessKey,Mode=TwoWay}" VerticalAlignment="Top"  PlaceHolder="AccessKey"/>

Style="{StaticResource ResourceKey=TextBoxPlaceHolder}" 

 

 

 

DialogAccessViewModel.cs

using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using Upbit.UpbitFunctions;
using Language;

namespace Upbit.ViewModels
{
    public class DialogAccessViewModel : BindableBase , ILanguage, IDialogAware
    {
        #region Lang
        private Language.Lang.Dialog lang;
        public Language.Lang.Dialog Lang
        {
            get { if (lang is null) lang = new Language.Lang.Dialog(); return lang; }
            set { SetProperty(ref lang, value); }
        }
        public void SetLanguage()
        {
            Lang.UpdateLang();
        }
        #endregion
        private string _title = "키등록";
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }
        

        #region Models
        private string _message;

        private string accesskey;
        public string AccessKey
        {
            get { return accesskey; }
            set { SetProperty(ref accesskey, value); }
        }
        public string Message
        {
            get { return _message; }
            set { SetProperty(ref _message, value); }
        }

        private string secretkey;
        public string SecretKey
        {
            get { return secretkey; }
            set { SetProperty(ref secretkey, value); }
        }


        #endregion Models   

        #region Prism
        private IEventAggregator _ea;

        #endregion Prism

        #region Event
        private DelegateCommand<string> _closeDialogCommand;
        public DelegateCommand<string> CloseDialogCommand =>
            _closeDialogCommand ?? (_closeDialogCommand = new DelegateCommand<string>(CloseDialog));

        public event Action<IDialogResult> RequestClose;
        #endregion Event

        #region Function

        #endregion Function

        #region Interface
        protected virtual void CloseDialog(string parameter)
        {
            Access.UpbitInstance.SetKeys(AccessKey, SecretKey);

            ButtonResult result = ButtonResult.None;

            if (parameter?.ToLower() == "true")
                result = ButtonResult.OK;
            else if (parameter?.ToLower() == "false")
                result = ButtonResult.Cancel;

            RaiseRequestClose(new DialogResult(result));
        }
        public virtual void RaiseRequestClose(IDialogResult dialogResult)
        {
            RequestClose?.Invoke(dialogResult);
        }

        public virtual bool CanCloseDialog()
        {
            return true;
        }

        public virtual void OnDialogClosed()
        {

        }

        public virtual void OnDialogOpened(IDialogParameters parameters)
        {
            //Message = parameters.GetValue<string>("message");
        }
        #endregion Interface


        public DialogAccessViewModel(IEventAggregator ea)
        {
            //언어 설정
            SetLanguage();
            _ea = ea;

            AccessKey = Access.UpbitInstance.GetAccessKey();
            SecretKey = Access.UpbitInstance.GetSecretKey();
        }


    }
}

 

 

IDialogAware

using Prism.Services.Dialogs;
using System;
namespace Dialog
{
    public interface IDialogAware
    {
        bool CanCloseDialog();
        void OnDialogClosed();
        void OnDialogOpened(IDialogParameters parameters);
        string Title { get; set; }
        event Action<IDialogResult> RequestClose;
    }
}

다이어로그로 띄워주기위해 PrismLibrary를 참고하여 작업했습니다. 아래 사이트를 참고해주세용.

https://prismlibrary.com/docs/wpf/dialog-service.html

 

Dialog Service | Prism

Dialog Service TODO: Intro Create Your Dialog View Your dialog view is a simple UserControl that can be designed anyway you please. The only requirement it has a ViewModel that implements IDialogAware set as it's DataContext. Preferably, it will utilize th

prismlibrary.com

 

 

생성자

public DialogAccessViewModel(IEventAggregator ea)
{
    //언어 설정
    SetLanguage();
    _ea = ea;

    AccessKey = Access.UpbitInstance.GetAccessKey();
    SecretKey = Access.UpbitInstance.GetSecretKey();
}

등록된 Key값들이 있을경우 데이터를 불러와서 UI쪽에 띄워줌.

 

 

다이어로그 확인

protected virtual void CloseDialog(string parameter)
{
    Access.UpbitInstance.SetKeys(AccessKey, SecretKey);

    ButtonResult result = ButtonResult.None;

    if (parameter?.ToLower() == "true")
        result = ButtonResult.OK;
    else if (parameter?.ToLower() == "false")
        result = ButtonResult.Cancel;

    RaiseRequestClose(new DialogResult(result));
}

형식적인 코드에서 살펴봐야할 코드는 

Access.UpbitInstance.SetKeys(AccessKey, SecretKey);

이거 한줄입니다.

Access.UpbitInstance에 키값을 등록해두는 이유는 정적변수로 선언하여 다른곳에서도 사용 할 수 있게 하기위해 저기다가 키값을 저장해둠.

 

 

반응형