How to reuse XAML code blocks without binding? - xaml

I have this XAML code block:
<StackPanel Orientation="Horizontal">
<TextBlock Text="Hello " />
<TextBlock Text="World" />
</StackPanel>
I would like to define this block (for example in /Common/CustomStyles.xaml) to include it on different XAML pages. How does it work?
I do not need a DataTemplate or something similar because the text is static.

You could put it in a user control and re-use it that way. If it's just the text you are interested in then you could use a resource file - this would update the textbox on any screen as long as it had the right uid.

Related

UWP Xaml Binding properties to an other page in a splitview

i need a little hand on this please,
Context : Windows 10 UWP Development, C#, Visual Studio 2017
I don't understand why I can pass INotify properties in my code, but i can not in the GEDContenuPage page view, that I want to add in the left pane of my SplitView.
<SplitView Grid.Row="1"
IsPaneOpen="{Binding IsGEDOpen}"
DisplayMode="Inline"
OpenPaneLength="{Binding GEDPaneWidth}">
<SplitView.Pane>
<Grid>
<v:GEDContenuPage Visibility="{Binding IsGEDOpen, Converter={StaticResource BoolToVisibilityConverter}}" />
</Grid>
</SplitView.Pane>
<ScrollViewer >
<Grid x:Name="mainGridData" />
</ScrollViewer>
</SplitView>
IsPaneOpen="{Binding IsGEDOpen}" will works and do its job (open or close left pane)
BoolToVisibilityConverter is in app.xaml and works well (tested somewhere else)
I have also created a DependencyPropertyin my page control :
<v:GEDContenuPage
Visibility="{Binding IsGEDOpen, Converter={StaticResource BoolToVisibilityConverter}}"
ext:GEDContenuPageExtension.ContenuId="2446"
/>
Same thing here, 2446 will works well, This will open my left pane with the right content.
But I i try to replace it with a Binding :
ext:GEDContenuPageExtension.ContenuId="{Binding GedContenuId}
It won't work.
I guess this is end of the week, and the only thing I need is a fresh beer to put things right in my mind...
For your information, the left pane will receive other page than GEDContenuPage. This is why I need a visibility property for each page.
Thanks for help,
OK this was a datacontext issue, this done the trick
<Grid Name="parentGrid">
<v:GEDContenuPage Visibility="{Binding DataContext.IsGEDOpen, Converter={StaticResource BoolToVisibilityConverter}, ElementName=parentGrid}" />

HubControl: Different files for each hub section on windows phone 8.1

I cannot find any information about how to structure the hubControl a little bit better. All the examples I can find put all the information in one xaml file. Am I missing something? I don't think that this gives you an easily maintainable code. The hub template creates the following code (stopping after the first section to abbreviate but there are four more to come in the sample code):
<Grid x:Name="LayoutRoot">
<Hub x:Name="Hub" x:Uid="Hub" Header="application name" Background="{ThemeResource HubBackgroundImageBrush}">
<HubSection x:Uid="HubSection1" Header="SECTION 1" DataContext="{Binding Groups}" HeaderTemplate="{ThemeResource HubSectionHeaderTemplate}">
<DataTemplate>
<ListView
ItemsSource="{Binding}"
IsItemClickEnabled="True"
ItemClick="GroupSection_ItemClick"
ContinuumNavigationTransitionInfo.ExitElementContainer="True">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,27.5">
<TextBlock Text="{Binding Title}" Style="{ThemeResource ListViewItemTextBlockStyle}" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</DataTemplate>
</HubSection>
How is it possible to have each section in a different xaml file?
Or are there other ways to get a better code structure?
I did something like this with the pivot control by adding the pivot items in code but the hub control does not have a content property and messing around with content templates seems a little bit too ugly. Does anyone have an idea? Thanks in advance.
Although I am still struggeling with passing the data context/ viewModel in some cases I guess the simple solution is to just create a new file (user control or page) and use something like
<HubSection Header="About">
<DataTemplate>
<local:AboutPage />
</DataTemplate>
</HubSection>
Thanks to everybody who had a look!
I think what you're looking for is a DataTemplateSelector. This allows you to write a custom mapping from instance to template in code.

Problems showing a web 3Dmodel viewer in the webBrowser control XAML

I am trying to show a url from Sketchfab and I can´t get the desired results. I need to show a page like this.
https://sketchfab.com/models/dGUrytaktlDeNudCEGKk31oTJY/embed?autospin=0.3&autostart=1&ui_infos=0
If you go there you can see that the model starts to spin automatically but if I put this Url in the source property of my WebBrowser control it shows the model but I doesn´t spin. The code that I am using is this:
<phone:PanoramaItem Header="{Binding Source={StaticResource LocalizedStrings}, Path=LocalizedResources.MainPagePanoramaItemViewTitle}" Orientation="Horizontal">
<Grid>
<phone:WebBrowser x:Name="WebBrowserViewer" Background="Black" HorizontalAlignment="Stretch" VerticalAlignment="Top" Height="450"
Source="https://sketchfab.com/models/dGUrytaktlDeNudCEGKk31oTJY/embed?autospin=0.3&autostart=1&ui_infos=0"
IsScriptEnabled="True" HorizontalContentAlignment="Stretch"/>
</Grid>
</phone:PanoramaItem>
Is posible reproduce this "autospin" and adjust de viewer to the WebBrowser control ?
Thanks a lot

How to use x:Uid in windows phone 8?

We can use x:Uid in windows-8 as
Where in .resw I have define
mainTitle.Text = "Your Name"
In this way TextBlock's text becomes Your Name.
How can I achieve the same in windows phone 8?
When I put mainTitle.Text in .resx it gives error.
You have to use binding in Windows Phone 8.
The most simple way to see this in action is to create a new project and take a look at MainPage.xaml. The binding is demonstrated in the following comment
For example:
Text="{Binding Path=LocalizedResources.ApplicationTitle, Source={StaticResource LocalizedStrings}}"
To localize some text, you bind the LocalizedStrings class (created with the project) which wraps your static Resource file.
<TextBlock Text="{Binding Path=LocalizedResources.ApplicationTitle, Source={StaticResource LocalizedStrings}}" />
The LocalizedStrings resource is already there in your App.xaml
<local:LocalizedStrings x:Key="LocalizedStrings"/>
You can use binding here
<TextBlock Text="{Binding Path=LocalizedResources.ApplicationTitle, Source={StaticResource LocalizedStrings}}"/>
For more about localization read How to build a localized app for Windows Phone
And to know about x:Uid read x:Uid Directive
Binding is one solution but simply you can do the following.
If you have the following AppBarButton
<AppBarButton
Name="AppBarButtonMore"
x:Uid="appbar_more_title"
Click="AppBarButtonMore_Click"/>
add the string file with the key appbar_more_title.Label to your resources and your button will take that string as label property.This is also valid for any UI controls.

Silverlight - displaying graphical resources within a button

Has anyone solved this: displaying graphical resources within a button
create a resource, for instance a rectangle
<UserControl.Resources>
<Rectangle x:Key="myRectangle" Fill="Red" Height="100" Width="100"/>
</UserControl.Resources>
then set the content of the button to the resource
<Button Content="{StaticResource myRectangle}"/>
when you build inside blend 4 RC you get the error "Value does not fall within the expected range."
Visual studio does not show this error.
When you run the site the button doesn't show any content.
This technique works in WPF without problems.
Anyone got any ideas?
This can be done by directly setting the shape as the button's content. For eg:
<Button Height="120" Width="120">
<Rectangle Fill="Red" Height="100" Width="100"/>
</Button>
FrameworkElement.Resources are generally used for storing nonvisual elements, brushes, etc. For your case (I think) you'll need to store your xaml as a data template, again not sure if this works with Buttons, it is used for things like ListBoxes. See here:
resources description on msdn. The link further contains pointers to information for Data Templates etc.