Let a MainPage.cs create in Xamarin in Visual Studio - macos

I am currently doing a visual studio xamarin course and I noticed that when, in the videos, a new Blank Forms App is created, the IDE creates an App.cs and MainPage.cs.
When I create one, the IDE creates a App.XAML with App.Xaml.cs and MainPage.XAML with MainPage.Xaml.cs.
XAML is where the design is done are XAML.CS where the logic is done or I am wright.
My question is, why or where come this change from? Why doesnt create XAML files in the course videos? An update or just a configuration difference?

This is just a difference in the templates provided in Visual Studio, when the video was shot, versus today.
It is up to you whether you create your view in the XAML file or not. However, it would be the usual place to design the UI. However, it is very opinion based, whether you should put any logic into the xaml.cs file. Some people would say you should follow the MVVM pattern and avoid putting anything but view and binding related stuff in it. Other people are more pragmatic and would allow logic to be put there.

Related

Looking for ways to edit Xamarin code on the run

This question could be split in 2:
is there any way to update xaml form markup in runtime and see the changes after re-entering the form? Similar to the way we can update asp.net pages in runtime
is there any way to update xaml.cs class in runtime? If I try, it shows me deceptive message
which is not true, as the change is small and compilable
Another strange thing displayed after that is the error in the Error List window
However project is already build on .net standard 2.1.0.0, and can see it in the project properties
XAML
Using XAML Hot Reload, it is possible to update your XAML during runtime and see the updates in real-time on the device/simulator.
C#
It is not currently possible to update and see your C# code changes during runtime.
The Xamarin engineering team is working on adding this feature and ou can follow their progress here: https://developercommunity.visualstudio.com/idea/650684/c-hot-reload-xamarin.html

How can I programmatically add xaml elements in Visual Studio 2017?

I have a C++/winrt project with a complex and dynamic xaml interface created in C++ code. Now I am moving to the latest VS 15.9.0 Preview 3, which has platform support for C++/Winrt and also allows use of the xaml designer in such a project. But I don't want to use the designer and have turned it off in Tools/Options/Xaml. The result is that none of my programmatic xaml elements appears. The project seems to expect me to enter these elements in a xaml code page, rather than using C++, e.g. Grid(), StackPanel(), view.RowDefinitions.Append(), view.SetRow() etc. The GeneratedFiles folder is now full of items that were not present in the previous project, yet can't be removed. Is it still possible to use the C++ interface for xaml, and what must be done to enable it if so? Thanks.
Ryan is correct: C++/winrt does support programmatic creation of xaml, and it works great. With the help of a couple of c++/winrt guys at MS I think I also know why my code was not doing anything. In the former version of my app I had declared MainPage as a C++ class, not a struct, and had assigned the starting Grid for the xaml by getting the current Window and setting currentWindow.Content(theGrid). But in the new template app MainPage is a struct, which might matter, and while setting window.Content that way no longer works, this does: this->Content(theGrid). Leaving aside some irrelevant issues about declarations in the BlankApp, this I think is the answer. Programmatic xaml works if you set that initial content as above.
Unfortunately, this is not the intended way to use this UI system. XAML-based UI systems are descendants of WPF, which relies on the Model-View-ViewModel (MVVM) pattern.
This pattern intends three types of classes to make up your application: Views, which are primarily written in XAML, and only deal with displaying data they are given; ViewModels, which are the wrapper and translator to give the views data, and to give the models commands; and lastly, Models, which are your backend business-logic classes.
Your instinct to not trust the designer is reasonable - it generates messy and unidiomatic XAML code. But it is an excellent way to preview the way your XAML code looks.
To get back to your specific situation, there are real problems in the library's API that will be serious roadblocks to programmatically define a UI in C++. Instead, you will want to use XAML to declare the UI. Adding and removing grid column definitions is not something that is well-supported, but using StackPanels and DockPanels is the normal way to do this.
If you have more specific questions, feel free to open a new question here, but do bear in mind that you may want to search first under the tags mvvm and wpf in addition to xaml, c++-winrt, and winrt.
If you have more questions that are rather broad and may be too broad for the main site here, feel free to join the WPF channel on chat, but bear in mind that most of us don't have experience in WinRT specifically.

How to link XAML file to Xamarin.form Previewer

How do I connect XAML file to Xamarin.form Previewer
as you can see this XAML file not linked to previewer
As mentioned here, you will need to assign some static data to your view for the Previewer when using Bindings. You can do so by assigning a static data context. James Montemagno has already described how to do so here.
If that doesn't solve the issue at hand, then Xamarin is already aware that some issues may exist since the Previewer is still only in preview:
Project should be built (compiled) before attempting to preview XAML files.
The Designer Agent must be set-up the first time you preview a XAML file - a progress indicator will appear in the Previewer, along with progress messages, until this is ready.
Try closing and re-opening the XAML file.
If the solutions as mentioned above doesn't solve the issues you experience, I would recommend that you get in touch with Xamarin. As mentioned, the Previewer is still in preview, so issues may still exist.

Xamarin Forms Forms Content Page and Forms Content Page XAML

ContentPage or ContentPage XAML ,Which is better to use for page design while developing Xamarin Forms application? Is there any advantage for one over another in app performance?
XAML vs code is entirely opinion based.
The general opinion seems to be that XAML as a markup language is much better suited and provides a much cleaner separation of the UI from the app logic.
I personally would never think of doing pages in code, XAML all the way.
Another point in favor of XAML is that if you use events or reference other controls (which, let's face it, every app does) you cannot even nest a bunch of initializers to build out a structured UI, you have to assign controls to named variables to be able to reference them. In XAML, you can add an x:Name at any point and you have a reference to that control from both code and the rest of the XAML markup.
The only thing you can't do in XAML is to dynamically programatically generate the layout or adjust it based on some runtime variables. But even here, it's best to layout the template in XAML, and then in code behind you can use the constructor (or override onappearing, if you want the page to change each time it's navigated to), and fill out the dynamic part from c# code.
Performance wise, there used to be a slight advantage to code pages, as XAML had to be parsed each time during runtime. However, for a while now, Xamarin.Forms has supported compiling the XAML files ahead of time, which means that they are more or less identical.

XAML fragments and the Visual Studio XAML designer

I'm new to XAML and likely not even thinking about this problem in the right way, but...
Basically I want a little XAML fragment that I can inject into various UserControls under some circumstances. The XAML just shows a small tag at the side of the control using a Border and a TextBlock.
It would be easy enough to cut and paste this to each control, but that feels clumsy and will be a pain any time I want to update it. Sure, I could do this at runtime in the control base class, but I would rather use the designer. I could make the tag a UserControl in its own right, but that sounds needlessly heavyweight too.
So is there some way of making little XAML fragments in the designer that I'm missing? I'm thinking there it would just have (in this case) the Border at the root of the XAML document.
This is for a Windows 8 store app using VS2013.
Have you tried XAML Code Snippets? Tim Heuer has a nice post with examples here:
http://timheuer.com/blog/archive/2013/07/08/xaml-code-snippets-for-visual-studio.aspx

Resources