none
[WPSL] Create a common library in wp8 RRS feed

  • Question

  • Hi All,

    I am new in windows phone 8.I have a requirement:I want to create a common library(.CS and xaml). Now I want to access both code and xaml from other project.Hoe is it possible in wp8?

    Real life example:We create a common library. All  banks integrate this common library and perform some common operation. 

    Friday, November 27, 2015 9:55 AM

Answers

  • Hi Lion,

    I have used your first idea, Create two different project. One is "main project" and another is "CommonLibrary".Now I go to the Reference manager  and add solution project, add the common library project. Now I used this code to perform the functionality"

    Main Project:

    NavigationService.Navigate(new Uri("/CommonLibrary;component/MainPage.xaml?keycode=" + keyCode + "&keyXmlPayload=" + keyXmlPayload + "&controls=" + controls + "&configuration=" + configuration + "&salt=" + salt + "&payInfo=" + payInfo + "&sandBoxKey=" + HttpUtility.UrlEncode(sandBoxKey), UriKind.Relative));

    CommonLibrary:

      protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
            {
    
    
                IsolatedStorageSettings isoStoreSettings = IsolatedStorageSettings.ApplicationSettings;
               
    
                if (NavigationContext.QueryString.ContainsKey("keycode") && NavigationContext.QueryString.ContainsKey("keycode"))
                {
                    string Keycode = NavigationContext.QueryString["keycode"];
                    
                    string KeycodeAndXML = NavigationContext.QueryString["keyXmlPayload"];
                    XMLParsing(KeycodeAndXML, Keycode);
                }
    
                if (NavigationContext.QueryString.ContainsKey("controls"))
                {
                    controlsString = NavigationContext.QueryString["controls"];
                    ControlParsing(controlsString);
                }
    
                if (NavigationContext.QueryString.ContainsKey("configuration"))
                {
                    string BankDetails = NavigationContext.QueryString["configuration"];
                    BankDetailsPArsing(BankDetails);
                }
    
                if (NavigationContext.QueryString.ContainsKey("salt"))
                {
                    SaltDetails = NavigationContext.QueryString["salt"];
                    SaltObject SaltList = JsonConvert.DeserializeObject<SaltObject>(SaltDetails);
                    TransID = SaltList.salt.txnID;
                    TransAmount = SaltList.salt.txnAmount;//Required to add on the text block
    
                }
    
                if (NavigationContext.QueryString.ContainsKey("sandBoxKey"))
                {
                    sandBoxKey= NavigationContext.QueryString["sandBoxKey"];
                   
                }
    
    
                if (NavigationContext.QueryString.ContainsKey("payInfo"))
                {
                    string PayInfo = NavigationContext.QueryString["payInfo"];
                    PayInfoPArsing(PayInfo);
                }
    
            }
    
    This is Right or wrong way please suggest?

    Tuesday, December 1, 2015 5:20 AM

All replies

  • I'm just a newbie and same as you, looking for a solution for sharing codes. I'm currently looking at this. I'm using Visual Studio 2015 and there is a template to build a portable class library.

    Let's share our findings here shall we!

    This is my first 11 months playing with Windows Store and Window Phone development and coming from a different programming language, I tend to look for similarities between development tools.

    eg. in Microsoft Visual FoxPro, I use a function FILE("path/filename.extension") to check whether file exist or not. Not finding similar function in C#/UWP, I created a helper method to do this and store the method and some other methods in a C# file, name as myRoutines.cs

    In MyRoutines.cs, I store all my new method (to help me with my transition) in a single class called myVFPs.

    namespace MyNameSpace.Common
    {
    
        class myVFPs
        {
            public async static Task<bool> File(StorageFolder paraFolder, string paraFilename)
            {
                try
                {
                    await paraFolder.GetFileAsync(paraFilename);
                    return true;
                }
                catch (FileNotFoundException)
                {
                    return false;
                }
            }
        }
    }

    Whenever I want to check for a file, I use

    using MyNameSpace.Common;

    .

    .

    . bool fileExist = await myVFPs.File(myStoragefolder, "filename.txt");

    .

    .

    .

    Being very new, I'm still missing one step. That is to create my own shareable collection of functions that I can use in any future projects. Currently, my file, MyRoutines.cs is limited to one single project and if I want to use them in different project, I have to make a copy and change to namespace of the code to be the same as the new project's namespace.

    Hence, the search and finding the link (above) concerning Portable Class Library.

    Any experts can advice @Sudip Das1 and me on this. Point us in the right direction, pls.

    Thanks.


    I'm at the advance level of my experience. In life that is, based on my age. Still learning the changing ways of programming. I do what I can to give a helping hand but sometimes due to my limited knowledge and understanding the issues, by trying to help I do make things worse and more confusing that the current problem. Please accept my apologies.


    • Edited by Nasarudin Harun Saturday, November 28, 2015 6:56 AM edit sample code
    Saturday, November 28, 2015 6:55 AM
  • Hi,

    If "other project" is Windows Phone 8 project too, you just need to create a Windows Phone 8 Class Library and "other project" need to reference to it to use your shared code.

    Another solution is creating a Portable class library (PCL). For more information about creating PCL, please refer to this site: https://msdn.microsoft.com/en-us/library/gg597391%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    Hope this helps!

    Saturday, November 28, 2015 6:53 PM
  • Hi Lion,

    I have used your first idea, Create two different project. One is "main project" and another is "CommonLibrary".Now I go to the Reference manager  and add solution project, add the common library project. Now I used this code to perform the functionality"

    Main Project:

    NavigationService.Navigate(new Uri("/CommonLibrary;component/MainPage.xaml?keycode=" + keyCode + "&keyXmlPayload=" + keyXmlPayload + "&controls=" + controls + "&configuration=" + configuration + "&salt=" + salt + "&payInfo=" + payInfo + "&sandBoxKey=" + HttpUtility.UrlEncode(sandBoxKey), UriKind.Relative));

    CommonLibrary:

      protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
            {
    
    
                IsolatedStorageSettings isoStoreSettings = IsolatedStorageSettings.ApplicationSettings;
               
    
                if (NavigationContext.QueryString.ContainsKey("keycode") && NavigationContext.QueryString.ContainsKey("keycode"))
                {
                    string Keycode = NavigationContext.QueryString["keycode"];
                    
                    string KeycodeAndXML = NavigationContext.QueryString["keyXmlPayload"];
                    XMLParsing(KeycodeAndXML, Keycode);
                }
    
                if (NavigationContext.QueryString.ContainsKey("controls"))
                {
                    controlsString = NavigationContext.QueryString["controls"];
                    ControlParsing(controlsString);
                }
    
                if (NavigationContext.QueryString.ContainsKey("configuration"))
                {
                    string BankDetails = NavigationContext.QueryString["configuration"];
                    BankDetailsPArsing(BankDetails);
                }
    
                if (NavigationContext.QueryString.ContainsKey("salt"))
                {
                    SaltDetails = NavigationContext.QueryString["salt"];
                    SaltObject SaltList = JsonConvert.DeserializeObject<SaltObject>(SaltDetails);
                    TransID = SaltList.salt.txnID;
                    TransAmount = SaltList.salt.txnAmount;//Required to add on the text block
    
                }
    
                if (NavigationContext.QueryString.ContainsKey("sandBoxKey"))
                {
                    sandBoxKey= NavigationContext.QueryString["sandBoxKey"];
                   
                }
    
    
                if (NavigationContext.QueryString.ContainsKey("payInfo"))
                {
                    string PayInfo = NavigationContext.QueryString["payInfo"];
                    PayInfoPArsing(PayInfo);
                }
    
            }
    
    This is Right or wrong way please suggest?

    Tuesday, December 1, 2015 5:20 AM
  • Hi Sudip Das1,

    Yes, in my mind it should be a correct way and I have tested your code in my side, it works fine.

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, December 9, 2015 3:13 AM
    Owner
  • Hello,

    Yes, you are doing right. Your code should work

    Wednesday, December 9, 2015 2:37 PM