In my opinion, the animation is too long and too "jumpy" - I'd like to remove it or make it more subtle. Possible?
I think you have to re-template the ChildWindow. Check this link so you can copy the default template and make your desired changes to the animation.
Here's the full recipe:
Extract the Child Window Template. I like to use Blend for this: create an empty project of the same type as your target project, put a child window directly to the main page. Select Edit Template->Edit a copy... Blend creates a style for the ChildWindow.
In your target project, add a new "Silverlight Resource Dictionary". name it "ChildWindowResources". Copy the entire style from the Blend project to the ChildWindowResources.xaml. Remove the "x:Key="ChildWindowStyle1" from the copied style.
In your target's project App.xaml, add the "ResourceDictionary" section thet looks like this:
-
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="/MyApp;component/ChildWindowResources.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Now you can edit the template and comment out these pesky animation sections under VisualState x:Name="Open" and VisualState x:Name="Closed"
Related
I am trying to have the ability for several core colors throughout the app to be changeable on the fly by the user. My current idea is to have a single SolidColorBrush that is my "theme color" and then to reference that throughout my main styles.
<SolidColorBrush x:Key="ReflectiveColor1" Color="#FF0E777B"></SolidColorBrush>
This snippet is stored in a resourcedictionary that is reference in my App.xaml
<Application.Resources>
<ResourceDictionary Source="DictionaryAlienwareTheme.xaml">
</ResourceDictionary>
</Application.Resources>
When I try and access the color in my App.cs I get an error
Application.Current.Resources["ReflectiveColor1"] = Colors.Black;
Error:
System.Runtime.InteropServices.COMException: 'No installed components were
detected.
Local values are not allowed in resource dictionary with Source set
Is there anyway to make this work? I'm assuming this error is because it doesn't want us to modify styles stored there, but I don't know the workaround.
Firstly, in your App.xaml if you want to reference the DictionaryAlienwareTheme.xaml you defined by yourself you need to merge the dictionaries to current app as #AVK said. More details please reference "Merged resource dictionaries" section of this article.
Secondly, after you use the merged resource, if you want to update the theme resource code behind you may need to use Application.Current.Resources.MergedDictionaries to visit the merged dictionaries instead of Application.Current.Resources. For example,
public MainPage()
{
this.InitializeComponent();
var mergedDict = Application.Current.Resources.MergedDictionaries.FirstOrDefault();
((SolidColorBrush)mergedDict["ReflectiveColor1"]).Color = Colors.Red;
}
By the way, as above code snippet showed, ReflectiveColor1 is SolidColorBrush you cannot set color directly.
Application will not be able to find the Resource ReflectiveColor1 until you merge all resource dictionaries together.
Change your App.xaml to Merge the dictionaries. Like below.
<Application.Resources>
<ResourceDictionary >
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="DictionaryAlienwareTheme.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Then you should be able to access the Resource in the same way that you were doing before.
I am new to Xamarin development. I created new Xamarin XAML App(Xamarin.Forms Portable). In Portable Project there where MainPage.Xaml by default. To create MVVM Model I created three new Folders- Views, ViewModels, and Models. Now I added new MainPage.Xaml in Views folder and was going to delete the default MainPage.Xaml page. But here I see some difference in both pages. The default MainPage.Xaml have xmlns:local="clr-namespace:Test" but the new MainPage.Xaml does not. Again the new MainPage.Xaml have <Label Text="{Binding MainText}" VerticalOptions="Center" HorizontalOptions="Center" /> but the default one does not. The screenshots are:
What does these MarkUp mean.Why there is a difference. Does something needs to be changed. Can I delete the default MainPage.Xaml or should i copy it in Views.Does I need to copy the Markup from the default Page to the new one. If so why?
Thanks in advance
Both pages are identical, and will display in the same way.
On the second, there's an additional Xml namespace declaration:
xmlns:local="clr-namespace:Test"
It's only a declaration. You could remove it, or add it to the other page without effect. It's purpose is to be able to reference custom views declared in the current assembly and in the namespace (c# namespace, this time) Test, like this:
<ContentPage
...
xmlns:local="clr-namespace:Test"
x:Class="Test.MainPage">
<local:MyAwesomeView />
</ContentPage>
Awesome that you have decided to start with Xamarin and Xamarin.Forms!
While I understand you might be having these questions, this is some very basic XAML knowledge. The short answer is: you don't need to worry about it.
The long answer:
The reason that there is a difference in these pages is simply because it's just a template and whoever at Xamarin created the template for the project can be a different person than who created the template for a new XAML page. So they solved it different ways. Or maybe he had a good/bad day, who knows.
The Label in the first page is simply there to show you how to get started and so you won't start with an empty screen.
The extra namespace xmlns:local="clr-namespace:Test" is actually redundant in this new page but is already there so you can use the classes in your project.
It is actually the equivalent of the using list at the top of your classes. So whenever you need something from a different namespace you have to declare it there. So if you create a folder 'Controls' you can add a attribute xmlns:controls="clr-namespace:Test.Controls".
Note how I changed local to controls, this is the prefix you will use to define your instance. Also I have added the right namespace Test.Controls. Now if you want te show something on screen, in your XAML from the controls namespace, go like this:
<ContentPage xmlns:controls="clr-namespace:Test.Controls" x:Class="Test.MainPage">
<!-- some stuff here -->
<controls:ReusableControlHere />
</ContentPage>
Where ReusableControl can be your own version of a Label, Button or virtually anything.
I want to change the default style or theme of the pivot control and I've found some really cool samples online, but i don't know where to place them in.
should i make a new file or edit an existing one?
You can create a new XAML ResourceDictionary in the project, where you can place these styles.
After that you have to include this resource dictionary as merged dictionary in the App.xaml:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ms-appx:///MyFolder/MyResourceDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Of course you don't have to create seperate file for these resources, you can place these to any Resources collection. Like to the Application.Resources in App.xaml or to the Page.Resourcesin the page's xaml.
A style will be default to the target control, if you don't provide any x:Key="" to them in the resource collection. These are implicit styles.
I want to make ComboBoxes in my Windows 10 App to have theme colours (One have a green theme, one with an orange theme...etc). I can do it by making different styles for each different colour, but that adds a huge amount of markup and it's a mess to manage. Because they're also the styles, I read that bindings won't work because styles are sealed once they're initialized.
Has anyone figured out a way to just change colours of a style without having to make multiple separate styles?
WPF has some nice markup for this very problem. You can use DynamicResource to allow the style to refresh every time the resource gets changed. Here's an example:
<SolidColorBrush x:Key="ColourAccent">#448AFF</SolidColorBrush>
<Style TargetType="Button">
<Setter Property="Background" Value="{DynamicResource ColourAccent}"/>
</Style>
This of course is a very simple implementation of this method, but essentially the DynamicResource is a reference to ColourAccent that, if the resource was to change, will automatically reflect the change in the style.
Now, changing the resource is a slightly different problem, for theming your styles, you need somewhere to put all of your colours, the solution here is to use multiple ResourceDictionaries and using MergedDictionaries. Let me show you what I mean:
Firstly, add a folder to your project called Themes, it'll just make things easier. Also, add that Style I mentioned earlier to either the Window.Resources or App.Resources, it's pretty important.
You need to add a ResourceDictionary file to the folder (Call it Amber.xaml), it'll look something like this:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<!-- Amber -->
<SolidColorBrush x:Key="ColourAccent">#FFC107</SolidColorBrush>
</ResourceDictionary>
Here is a simple resource dictionary, it contains one resource called ColourAccent, now we need to create another one (Call it Blue.xaml), but with a different colour:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<!-- Blue -->
<SolidColorBrush x:Key="ColourAccent">#448AFF</SolidColorBrush>
</ResourceDictionary>
The magic here will become clear soon enough. Having multiple resource dictionaries with resources under the same name will allow you to create themes. The method of which is to swap the current resource dictionary for a new one, and by doing so, the DynamicResource will notice there has been a change and it'll refresh the control styles.
Now you have two resource dictionaries, you need to work out which dictionary to use. Firstly, we need to define the default resource dictionary that your application is going to use, you need to declare this in App.xaml.
<Application ...>
<Application.Resources>
<ResourceDictionary>
...
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/YourNamespace;component/Themes/Amber.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
So here you'll see the MergedDictionaries element, this is where the magic happens. If you run the app now, you should see that any buttons will have a background colour of Amber. Cool, huh?
Now it gets a little bit trickier, we will be removing the resource dictionary that is currently in the dictionary and replacing it with a different theme (a different resource dictionary).
Here is some C# to do that:
Disclaimer: I just threw this code together, there is almost certainly a better way, but you get the idea. Put the following code in a mouse click event, or somewhere where you can step it through to see what's happening.
Firstly, remove the current theme from the merged dictionaries.
//Find the current dictionary
ResourceDictionary oldDictionary = App.Current.Resources.MergedDictionaries.FirstOrDefault();
//If we found one, remove it.
if (dictionary != null)
App.Current.Resources.MergedDictionaries.Remove(oldDictionary);
Now we just need to add a different resource dictionary. I'm just going to add the Blue theme for the sake of an example:
//Declare some variables.
string folderPath = "/YourNamespace;component/Themes/";
string desiredTheme = "Blue";
//Create the new resource dictionary
ResourceDictionary newDictionary = new ResourceDictionary();
newDictionary.Source = new Uri(string.Format("{0}{1}.xaml", folderPath, desiredTheme), UriKind.RelativeOrAbsolute);
//Add the resource dictionary to the merged dictionaries.
App.Current.Resources.MergedDictionaries.Add(newDictionary);
Now if all goes well, the background for any buttons in your app should now be Blue. Hooray!
Using this method, which in essence is creating multiple resource dictionaries each with resources under the same name, you can create multiple themes for your application. This isn't restricted to colours, you can have entire styles which are theme specific, where one theme might display a button in a completely different way as another style. Experiment and see what you can come up with. Good luck!
I am reading up about the ResourceDictionary and have come to a confusing point.
It appears I can only have 1 ResourceDictionary per XAML. So, if I wanted to use more than one, I can Merge the ResourceDictionaries.
If I can merge dictionaries then where should 'global' styles live? I could have an ApplicationResourceDictionary with all the styles which are to be consistent throughout my application OR, I could save this information into the App.xaml file. both appear to be valid options but I don't know if that is the case.
Is this about personal choice or is one better than the other? It would appear keeping them in ResourceDictionaries is better because all styles are together (within the dictionaries) instead of splitting some in XAML pages.
Our current solution has 100+ projects in it. Each needing access to a few Resource Dictionaries with global resources for themes and uniformity etc. What I do for it is have the resource dictionaries centrally located in one project the others reference, in this case we call it "infrastructure" then I supply the dictionaries to each proj directly via their own app.xaml with merged dictionaries like for example;
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Our.Client.Infrastructure;component/Resources/Styles/ResDictName1.xaml" />
<ResourceDictionary Source="/Our.Client.Infrastructure;component/Resources/Styles/ResDictName2.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Which so far works splendidly, the way I make the styles apply global though is specifying them as such at the bottom of one of our custom Resource Dictionaries and remove the same declaration from the Default Resource Dictionaries. So for example if you find;
<Style BasedOn="{StaticResource DefaultButtonStyle}" TargetType="Button" />
in your Default CoreStyles.xaml or SdkStyles.xaml or whatever they may be, I just remove it, and move that declaration over to the bottom of our custom Resource Dictionary and change it accordingly like;
<Style BasedOn="{StaticResource OurSuperAwesomeCustomNewGlobalButtonStyle}" TargetType="Button" />
and voila... Any Button thereafter inherits our custom style by default instead of the original default template. The advantages of having just one or two Resource Dictionaries for your entire solution become clear real quick once you adopt it. That is, provided the template actually needs to be globally available. If you're using a template for something adhoc that only pertains to the view its used on, keep it in that view explicitly, no need to keep it somewhere else if nothing else needs it.
Hope this helps.