|
| 1 | +# R/WinRT for Windows App SDK |
| 2 | + |
| 3 | +R/WinRT is ease-to-use resources access for the MRTCore resource files. |
| 4 | + |
| 5 | +[Source code][source] | [Package (NuGet)][package] |
| 6 | + |
| 7 | +## Getting started |
| 8 | + |
| 9 | +### Install the package |
| 10 | + |
| 11 | +Install the R/WinRT with [NuGet][nuget]: |
| 12 | + |
| 13 | +```cmd |
| 14 | +nuget install Mntone.RWinRT |
| 15 | +``` |
| 16 | + |
| 17 | +## Examples |
| 18 | + |
| 19 | +### [C#/WinRT] Get the resource |
| 20 | + |
| 21 | +Define "Hello, World!" as the key "HelloWorld" in Resources.lang-en-US.resw. |
| 22 | + |
| 23 | +```cs |
| 24 | +using System; |
| 25 | + |
| 26 | +namespace RWinRTCsTest |
| 27 | +{ |
| 28 | + class Program |
| 29 | + { |
| 30 | + static void Main(string[] args) |
| 31 | + { |
| 32 | + var text = R.HelloWorld.Value; |
| 33 | + Console.WriteLine(text); // [Output] Hello, World! |
| 34 | + } |
| 35 | + } |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +### [C#/WinRT] Get the formatted resource |
| 40 | + |
| 41 | +Define "I have {0} apples!" as the key "IHaveNApples" in Resources.lang-en-US.resw. |
| 42 | + |
| 43 | +```cs |
| 44 | +using System; |
| 45 | + |
| 46 | +namespace RWinRTCsTest |
| 47 | +{ |
| 48 | + class Program |
| 49 | + { |
| 50 | + static void Main(string[] args) |
| 51 | + { |
| 52 | + var count = 4; |
| 53 | + var fmtText = R.IHaveNApples.Format(4); |
| 54 | + Console.WriteLine(fmtText); // [Output] I have 4 apples! |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +### [C#/WinRT] Change current language |
| 61 | + |
| 62 | +Define "日本語 (Japanese)" as the key "CurrentLanguage" in Resources.lang-ja-JP.resw. |
| 63 | + |
| 64 | +```cs |
| 65 | +using System; |
| 66 | + |
| 67 | +namespace RWinRTCsTest |
| 68 | +{ |
| 69 | + class Program |
| 70 | + { |
| 71 | + static void Main(string[] args) |
| 72 | + { |
| 73 | + RWinRT.ResourceManager.ChangeLanguage("ja-JP"); |
| 74 | + |
| 75 | + var language = R.CurrentLanguage.Value; |
| 76 | + Console.WriteLine(language); // [Output] 日本語 (Japanese) |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +### [C++/WinRT] Get the resource |
| 83 | + |
| 84 | +Define "Hello, World!" as the key "HelloWorld" in Resources.lang-en-US.resw. |
| 85 | + |
| 86 | +```cpp |
| 87 | +#include <iostream> |
| 88 | + |
| 89 | +#include "res.g.h" |
| 90 | + |
| 91 | +int main() { |
| 92 | + ResourceManager<R> manager; |
| 93 | + ResourceContext<R> context { manager.Context() }; |
| 94 | + winrt::hstring text { context.Value<R::HelloWorld>() }; // [Output] Hello, World! |
| 95 | + std::cout << text << std::endl; |
| 96 | + return 0; |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +### [C++/WinRT] Get the formatted resource |
| 101 | + |
| 102 | +Define "I have %d apples!" as the key "IHaveNApples" in Resources.lang-en-US.resw. |
| 103 | + |
| 104 | +```cpp |
| 105 | +#include <iostream> |
| 106 | + |
| 107 | +#include "res.g.h" |
| 108 | + |
| 109 | +int main() { |
| 110 | + ResourceManager<R> manager; |
| 111 | + ResourceContext<R> context { manager.Context() }; |
| 112 | + int count { 4 }; |
| 113 | + winrt::hstring fmtText { context.Format<R::IHaveNApples>(count) }; // [Output] I have 4 apples! |
| 114 | + std::cout << fmtText << std::endl; |
| 115 | + return 0; |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +### [C++/WinRT] Change current language |
| 120 | + |
| 121 | +Define "日本語 (Japanese)" as the key "CurrentLanguage" in Resources.lang-ja-JP.resw. |
| 122 | + |
| 123 | +```cpp |
| 124 | +#include <iostream> |
| 125 | + |
| 126 | +#include "res.g.h" |
| 127 | + |
| 128 | +int main() { |
| 129 | + ResourceManager<R> manager; |
| 130 | + ResourceContext<R> context { manager.Context(L"ja-JP") }; |
| 131 | + winrt::hstring text { context.Value<R::CurrentLanguage>() }; // [Output] 日本語 (Japanese) |
| 132 | + std::cout << text << std::endl; |
| 133 | + return 0; |
| 134 | +} |
| 135 | +``` |
| 136 | + |
| 137 | +[source]: https://github.com/mntone/RWinRT/tree/main/src |
| 138 | +[package]: https://www.nuget.org/packages/Mntone.RWinRT |
| 139 | +[nuget]: https://www.nuget.org/ |
0 commit comments