티스토리 뷰

반응형

리소스를 이용해서 문자열 작업할 때 사용하는 방법이다.

 

1. DynamicResource

    /// <summary>
    /// 다이나믹 리소스
    /// </summary>
    public class DynamicResource : DynamicObject
    {
        /// <summary>
        /// 윈도우 리소스로더
        /// </summary>
        Windows.ApplicationModel.Resources.ResourceLoader _rl;

        /// <summary>
        /// 리소스 전체 이름
        /// </summary>
        public string ResourceName { get; set; }

        /// <summary>
        /// 이름으로 호출
        /// </summary>
        /// <param name="binder"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            string str = string.Empty;
            if (Windows.ApplicationModel.DesignMode.DesignModeEnabled == false)
            {
                if (_rl == null)
                {
                    _rl = Windows.ApplicationModel.Resources.ResourceLoader.GetForCurrentView(ResourceName);
                }

                str = _rl.GetString(binder.Name);
                if (string.IsNullOrEmpty(str) == true)
                {
                    str = string.Empty;
                }
            }
            else
            {
                str = binder.Name;
            }
            result = str;
            return true;
        }

        /// <summary>
        /// 프로퍼티로 호출
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string this[string id]
        {
            get
            {
                string str = string.Empty;
                if (Windows.ApplicationModel.DesignMode.DesignModeEnabled == false)
                {
                    if (_rl == null)
                    {
                        _rl = Windows.ApplicationModel.Resources.ResourceLoader.GetForCurrentView(ResourceName);
                    }

                    str = _rl.GetString(id);
                    if (string.IsNullOrEmpty(str) == true)
                    {
                        str = string.Empty;
                    }
                }
                else
                {  
                    //디자인 타임에서는 키값을 반환
                    str = id;
                }
                return str;
            }
        }
    }

 

2. App.xaml

xmlns:CPCommons="using:CrossPlatform.Infrastructure.StoreApp.Commons"

...

 

<CPCommons:DynamicResource x:Key="DResource" ResourceName="ScreenshotAutoCut.PCL.en.UIResources"/>

 

3. xaml에서 사용하기

 

      <TextBlock Style="{StaticResource SubheaderTextBlockStyle}" Grid.Row="1" Margin="0,10,0,0" TextWrapping="Wrap"
       Text="{Binding [tx_please_add_screenshots_folder], Source={StaticResource DResource}}"/>
      <TextBlock Style="{StaticResource TitleTextBlockStyle}" Grid.Row="2" Margin="0,10,0,0"
       Text="{Binding [tx_description], Source={StaticResource DResource}}"/>
      <TextBlock Style="{StaticResource BodyTextBlockStyle}" Grid.Row="3"
       Text="{Binding [tx_click_the_add_folder_button], Source={StaticResource DResource}}" >

4. cs에서 사용하기

            //리소스 타입 입력 - dynamic 키워드가 중요
            dynamic DR = App.Current.Resources["DResource"];

            var test = DR.tx_please_add_screenshots_folder;

         System.Diagnostics.Debug.WriteLine(test);


 

반응형
댓글