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
Related
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.
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.
Im just wondering what you use to get the vertical visualisation lines between XAML open and close tags, it makes the XMALso much easier to read.
I cant find a better place to ask this but im sure there might be, sorry if its broken the rules.
Do you mean the Visual Studio extension called "Indent Guides": https://visualstudiogallery.msdn.microsoft.com/e792686d-542b-474a-8c55-630980e72c30 ?
It puts little guides in your VS2013 (and perhaps 2012) that show indents in the XAML editor. If you didn't mean that, I'd still recommend a look as it makes things a lot easier to read!
Visual Studio WP 2010.
I'm using scrollviewer for text and images on a page, but in design view in VS I can only view the first part of the page which makes placing text and images difficult. I can take a guess and then run the app in the emulator, scroll down to see how far off I am, then go back in design view and try to set margins, height, etc appropriately. Is there another easier way to do this?
Just a guess: During creation set the ScrollViewer's VerticalOffset.
I found the answer elsewhere. In the top section of your xaml find d:DesignHeight="696" and change the 696 to something like 2000, etc. This will extent the viewing area. Anytime you reload or run the application it will default back, but it's easy to reset.
I'd like to add a search dialogue popup, really just a text box or something at the top right of the current document. I've messed around with the VSPackage builder and it's easy enough, but I'm having trouble finding out which UI elements I should be looking at.
A ToolWindow doesn't quite fit I don't think -- I'd like what I'm doing to be borderless. I tried just using a custom WPF window by itself but that doesn't play well with the IDE.
It sounds what you're after is an editor viewport adornment.
This is a type of extension that has a class inherited from Microsoft.VisualStudio.Text.Editor.IWpfTextViewCreationListener
To create an adornment, you can use the Editor Viewport Adornment project template under Visual C#\Extensibility.
Try this example:
http://www.codeproject.com/Articles/55196/Code-InfoBox-Visual-Studio-Extension-VSX-2010