Sharing a Style Between Apps & Projects

Nathan Sokalski 4,111 Reputation points
2021-01-28T00:09:16.73+00:00

I have a number of Style(s) that I want to use in multiple apps & projects. I basically need a project that contains Style(s) (or a ResourceDictionary) that can be accessed by the shared project of Xamarin.Forms. In UWP I was able to create & reference a project that contained a *.xaml file with the ResourceDictionary. Is there a way to do this in Xamarin.Forms?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,371 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 78,671 Reputation points Microsoft Vendor
    2021-01-28T03:19:27.327+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    I basically need a project that contains Style(s) (or a ResourceDictionary) that can be accessed by the shared project of Xamarin.Forms

    I tried this way to create a share project, then add ResourceDictionary, but When I use it in xamarin(I added the share reference).

       <ResourceDictionary>  
                   <!-- Add more resources here -->  
                   <ResourceDictionary.MergedDictionaries>  
                       <ResourceDictionary Source="/StyleProject;component/Dictionary1.xaml" />  
                   </ResourceDictionary.MergedDictionaries>  
                   <!-- Add more resources here -->  
               </ResourceDictionary>  
    

    But when I build my project, I get Resource "/StyleProject;component/Dictionary1.xaml" not found. it is not work.

    So you can create a CSS file for sharing style. Then you can copy this CSS file to other Projects,

    For example, I write a following css style. allow for targeting object types in Xamarin.Forms directly using a ^:

       ^ContentPage {  
           background-color: #B9BAA3;  
           padding: 15;  
       }  
         
       ^Label {  
           text-align: center;  
           font-style: bold;  
           font-size: 30;  
           color: #A22C29;  
       }  
         
       #LoginForm {  
           background-color: #D6D5C9;  
           padding: 15;  
           border-radius: 5;  
           margin-top: 20  
       }  
         
       ^Entry {  
           font-size: 30;  
           background-color: white;  
           margin: 10 0;  
           color: #0A100D;  
       }  
         
       ^Button {  
           font-size: 16;  
           color: #A22C29;  
           margin: 5 0;  
       }  
       ^Button.primary {  
           background-color: #902923;  
           color: #FFFFFF;  
           font-size: 20;  
           margin: 15 0;  
       }  
    

    And set the build action is Embedded resource

    If I use this style.

       <ContentPage.Resources>  
               <StyleSheet Source="Styles.css" />  
        </ContentPage.Resources>  
    

    For more detials, you can google :Styling Xamarin.Forms Apps with CSS -- Visual Studio Magazine

    Best Regards,

    Leon Lu


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.