Sunday, July 29, 2018

Xamarin Forms Storing some Json in a Standard Library

In one of my Xamarin forms app I wanted to include some default data for the app in Json format.

Rather than create a web service to load the data on the first run I went ahead and created a Json file to include the default data.

[
{"Question":"Name two key components that make up Sitecore XP","Answer":"Experience CMS, and marketing platform"}

etc...


I added the Json file to my Xamarin Forms standard library and set its build action to embedded resource





To extract the data I used a function like this.

        public ObservableCollection<QuestionPair> GetQuestions()
        {
            var assembly = typeof(DataService).GetTypeInfo().Assembly;
            Stream stream = assembly.GetManifestResourceStream("SitecoreFlashCards.questions.json");
            StreamReader streamReader = new StreamReader(stream);
            string json = streamReader.ReadToEnd();
            ObservableCollection<QuestionPair> questions = new ObservableCollection<QuestionPair>();
            var list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<QuestionPair>>(json);
            foreach(var item in list)
            {
                questions.Add(item);
            }

            return questions;
        }

First I created a stream so I can access the Json file in the standard library.  The I used a stream reader to get the text from the file and deserialize it with Json.net

The path to the resource is the class library default namespace and the path to the file and file name in the dll.

Replace DataService with the name of the class you are trying to get the Json file from.


Sunday, July 22, 2018

Using GitHub with Visual Studio for the Mac

First lets create a new Repository on GitHub .

For this demo I created a public repository that has a readme, and a Visual Studio Git Ignore file.




Now I created a simple .Net Core Console app to check into GitHub.  I checked use git for version control.



Once I created the app I built it to make sure it runs.   Now the first thing to do is create an initial commit to the local git repository.  In the version control menu select Review and Commit.


In Git Hub open the Repository you created. Press the clone or download button and copy the url for the repository.

In Visual Studio Version Control Menu select Manage Branches and Remotes



Add a new Remote source with the url from the GitHub website



I made a minor code change and Selected Review and Commit changes




In the dialog I selected Push to remote.  First time you connect to GitHub it will ask for you credentials




Once you push your changes you should see it in GitHub.  Make sure before you push your changes you update the solution from GitHub.