外部化 domainString Xamarin (android)

Externalize domainString Xamarin (android)

我正在用 Xamarin 编写一个应用程序,想知道是否可以外部化我在异常中使用的字符串。

例如,如果我写在我的代码中

throw new Exception("This is an error");

如何外部化错误消息 "this is an error"?

我已经尝试过类似的方法:

throw new Exception("@string/errormessage")

并在 Strings.xml 文件中添加了错误消息,但是没有用。 有人知道这是否可以做到吗?

假设这是在你的 values\Strings.xml:

<string name="Whosebug">Whosebug Custom Exception String</string>

您可以通过以下方式将其用作您的自定义异常消息:

button.Click += delegate {
   throw new Exception(this.Resources.GetString(Resource.String.Whosebug));
};