簡體   English   中英

如何訪問 xamarin.forms 中的 resx 文件?

[英]How access to resx file in xamarin.forms?

我整天都在嘗試訪問 resx 文件的資源字符串。 我嘗試這種方式:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="test.MainPage">

<StackLayout>
    <Label Text="{x:Static res:AppResources.String1}"/>
</StackLayout>

</ContentPage>

我直接在 test.android 項目中創建了一個 AppResources 文件,其中包含 String1 資源並公開設置。 如何在 xaml 上訪問此資源? 我嘗試了在互聯網上找到的各種方法,但沒有任何效果。

您需要為 resx 文件定義res命名空間並將其添加到頁面頂部:

<ContentPage 
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:res="clr-namespace:YourResxFile.Namespace"
    x:Class="test.MainPage">
    

有關本地化的更多信息,請參見Xamarin.Forms 字符串和圖像本地化

在您的情況下,您可以使用: xmlns:res="clr-namespace:KalendarzLiturgiczny"

我直接在 test.android 項目中創建了一個 AppResources 文件,其中包含 String1 資源並公開設置。 如何在 xaml 上訪問此資源?

創建一個 Xamarin.Forms 項目,然后在您的 Forms 項目中直接添加AppResources.resx 我創建了一個 App1 項目以供參考。

在此處輸入圖像描述

Xaml:定義res

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:res="clr-namespace:App1"
         x:Class="App1.Page3">
<ContentPage.Content>
    <StackLayout>
        <Label Text="{x:Static res:AppResources.String1}"/>
    </StackLayout>
</ContentPage.Content>
</ContentPage>

將 App1.Android 設置為啟動項目運行。

如果你想創建一個文件夾來放置 .resx 文件,你可以像下面這樣定義 resx。

xmlns:res="clr-namespace:App1.folderName"

在此處輸入圖像描述

在此處輸入圖像描述

在此處輸入圖像描述

我嘗試這種方式但不工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM