I'm trying to load a list of news from a website, so first I make a request to fetch the news (with the thumbnails), and using the Binding feature, I assign the Fetched news to my list box, which contains the image (ImageUrl).
<ListBox Name="lstNews">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Margin="0,0,12,12" Width="180" Height="180">
<Grid.Background>
<ImageBrush ImageSource="{Binding ImageUrl}" />
</Grid.Background>
<StackPanel Background="#AA000000" VerticalAlignment="Bottom" Height="60" >
<TextBlock TextWrapping="Wrap" VerticalAlignment="Bottom" TextAlignment="Center" Text="{Binding Title}" />
</StackPanel>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
It works fine, but the UI freezes until the images show up. how can I fix that?
Actually, BitmapImages by default do load on the UI thread, which would cause the blockage. In your xaml, you should be able to do something like:
<ImageBrush>
<ImageBrush.ImageSource>
<BitmapImage CreateOptions="BackgroundCreation" UriSource="{Binding ImageUrl}"/>
</ImageBrush.ImageSource>
</ImageBrush>
Making sure to specify BackgroundCreation for the CreateOptions. For more information, I personally found this blog post to be quiet useful.
Have you tested to make sure that your UI does not freeze when you comment out your ImageBrush line? I believe the automatic conversion creates a BitmapImage from your ImageUrl, which normally loads on a background thread, so your UI thread should not block. It is possible that something else altogether is blocking the UI, like excessive amounts of text that needs to be laid out or your data layer code that feeds the list with the data.
Related
I am trying to build my UWP app and currently am stuck with designer exceptions when trying to use DataTemplate with x:Bind in a Resource Dictionary.
I have created a Resource Dictionary "ItemTemplates.xaml" with a respective code-behind (to ensure x:Bind initialization). The file contains just one template:
<DataTemplate x:Key="HomeViewCategoryListItemTemplate" x:DataType="models:Category">
<Button Background="#88333333" Height="110" VerticalContentAlignment="Top" Padding="10" HorizontalAlignment="Stretch">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock FontWeight="Light" HorizontalAlignment="Center" Text="{x:Bind Name}" FontSize="{ThemeResource TextStyleExtraLargeFontSize}" />
<TextBlock Foreground="{ThemeResource ToolTipForegroundThemeBrush}" HorizontalAlignment="Center" Margin="0,10,0,0" Text="{x:Bind Description}" Grid.Row="1" TextAlignment="Center" TextWrapping="Wrap" />
</Grid>
</Button>
</DataTemplate>
Then I added this resource dictionary to App.xaml like this:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ms-appx:///Resources/Core.xaml" />
<resources:ItemTemplates />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Now the project is unusable, because designer throws weird exceptions, but when I Clean and Rebuild the project and navigate to the HomeView.xaml page, the designer shows just the default "ToString()" items (basically the list view contains just three times the text "Models.Categories") in the ListView and the ItemTemplate property of my ListView is underlined and shows the following error:
The resource "HomeViewCategoryListItemTemplate" could not be resolved.
When I navigate back to App.xaml, I see yet another underline there (of the <resources:ItemTemplates /> line) which says:
The property 'DataType' was not found in type 'DataTemplate'.
Both errors are non-sensical, because when I actually run the app, there are no issues and everything works perfectly. The only workaround I have found so far is to include the ResourceDictionary two times in both the classic way and the "compiled" way:
<ResourceDictionary Source="ItemTemplates.xaml" />
<resoures:ItemTemplates />
This solution works and then everything works both in design time and in run-time, but I really think it is quite messy and there has to be a better, safer approach or I am missing something trivial.
I am running Visual Studio 2015 Update 1 and have the newest UWP SDK installed. The project targets build 10240.
Edit:
Another exception that the designer very often throws and crashes completely:
Unable to cast object of type 'System.String' to type 'Models.Data.Categories.Category'.
According to the StackTrace output this happens inside the ItemTemplates.xaml.cs code - specifically the generated method ProcessBindings. Again, the project still compiles and runs normally, but the designer does not even bother trying to show the output.
For this current version, prefer Binding over x:Bind
an answer from a Microsoft engineers
I've got the same problems as you did, plus a ton of bugs when using x:Bind in Design time. Quickest way to fix: use Binding as old time. And when you release, if performance is under consideration, change the Binding to x:Bind
I started to use advertising control, but I'm going crazy trying to let it work.
I copied PivotPage.xaml from Microsoft Ad Control Sample and made it the starting page in my app, but ad control is not shown (I'm not able to see banner); if I try to run Microsoft app, banner is shown.
So I registered my app on pubCenter and I got an appId and a unitId and tried to use them in my app, but the result is the same: no banner!!
If I try to use my ids in Microsoft app, test banner is shown.
Why using Microsoft example I'm able to see banner and using same page in my app I can't?
Why using my ids, Microsoft app does not show correct banner?
Here is XAML
<!--Pivot Control-->
<controls:Pivot Grid.Row="0" Title="Ad Control Sample">
<!--Pivot item one-->
<controls:PivotItem Header="piv 1">
</controls:PivotItem>
<!--Pivot item two-->
<controls:PivotItem Header="piv 2">
</controls:PivotItem>
<!--Pivot item three-->
<controls:PivotItem Header="piv 3">
</controls:PivotItem>
</controls:Pivot>
<StackPanel Grid.Row="1" VerticalAlignment="Bottom" >
<TextBlock Text="This is the same ad."
TextWrapping="Wrap"
HorizontalAlignment="Stretch"
TextAlignment="Center" />
<my:AdControl Name="adControl1"
ApplicationId="test_client"
AdUnitId="Image480_80"
HorizontalAlignment="Center"
Width="480" Height="80" />
</StackPanel>
I finally managed to solve my problem and I want to share solution.
I add an event handler for ErrorOccurred on ad control and reading Microsoft.Advertising.AdErrorEventArgs e I realized that my manifest (WMAppManifest.xml) was missing
<Capability Name="ID_CAP_IDENTITY_USER" />
<Capability Name="ID_CAP_MEDIALIB" />
<Capability Name="ID_CAP_WEBBROWSERCOMPONENT" />
I have 2 tables Main and Maintest. I am using nhibernate to pull data from database and am joining 2 tables to fetch the fields from both.Now my final object has data from both the tables. Now when I debug my app I can see that I have 2 records from Main and 5 records from Maintest. But somehow I am not able to display records from Maintest.
<DataTemplate x:Key="myTaskTemplate">
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding FirstName}"/>
</StackPanel>
</DataTemplate>
</Window.Resources>
<StackPanel>
<ListBox ItemsSource="{Binding Main}" ItemTemplate="{StaticResource myTaskTemplate}" Height="200" Width="200" />
<toolkit:DataGrid ItemsSource="{Binding Main.Maintest}" Margin="3"
AutoGenerateColumns="False"
CanUserAddRows="False" CanUserDeleteRows="False"
CanUserReorderColumns="False" CanUserResizeRows="False">
<toolkit:DataGrid.Columns>
<toolkit:DataGridTextColumn Header="#"
Binding="{Binding Number}"/>
<toolkit:DataGridTextColumn Header="Airline"
Binding="{Binding Code}"/>
</toolkit:DataGrid.Columns>
</toolkit:DataGrid>
</StackPanel>
NHibernate Mapping:
<class name="Main" lazy="false">
<id name="ID" type="Int32">
<generator class="native"/>
</id>
<set name="Maintest" inverse="true">
<key column="Ticket" on-delete="cascade" />
<one-to-many class="Segment" />
</set>
....
I able to display listbox record but not toolkit records. Although I can see that for each Main record my object does have 3 or more records in Maintest.
Something doesn't add up...
ListBox.ItemsSource takes an IEnumerable of some kind - which means the property Main must be some kind of IEnumerable?
So, if what you're looking for is a Master-Detail kind of view - you need to change the XAML for the two controls like this - everything else should be fine:
<ListBox Name="Main" .../>
<toolkit:DataGrid ItemsSource="{Binding SelectedItem.Maintest,ElementName=Main}" .../>
This will make the DataGrid bind to the MainTest property of whatever obejct is selected in the ListBox.
Look to Bea Costa if you really need to get spiffy with Master-Detail scenarios.
Hope this helps!
My ViewModel has a property called Commands which is of type IDictionary.
For my data grid I have created a ControlTemplate for one of the fields using a button as follows:
<ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}">
<Button Style="{DynamicResource btnRemove}" Width="14" Height="14"
Content="{TemplateBinding Content} "
CommandParameter="{Binding ViewID}"
Command="{Binding Commands[AcknowledgeErrorCmd]}" />
<ControlTemplate.Triggers>
</ControlTemplate.Triggers>
</ControlTemplate>
Clicking on the button does nothing which tells me the binding did not work. However, an unstyled button added to the toolbar of the same window hosting this grid works, binds properly to this command. I guess my question is:
Hw do I bind the command property of a button used in a ControlTemplate to a ViewModel?
TIA.
I am not sure what the problem is but try to debug your solution and look into the output window with Debug selected in the combobox and you will see the errors that occur during binding. Maybe this will help you to the solution.
Provide me the error as a comment on this post if you don't understand it.
I did this instead:
<ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}">
<Border >
<TextBlock Margin="5">
<Hyperlink
CommandParameter="{Binding ElementName=root, Path=DataContext.ViewID}"
Command="{Binding ElementName=root, Path=DataContext.Commands[AcknowledgeErrorCmd]}">
<TextBlock Text="Acknowledge"/>
</Hyperlink>
</TextBlock>
</Border>
</ControlTemplate>
and that works fine. It may be related to the post Viko provided.
I'm having a problem with this this XAML... When I run it, it hangs because of the TextBox. (By "hangs" I mean that the hosting aspx page shows in the browser, but the usercontrol object will not appear on the page, and there are some little green bars in the bottom of the Internet Explorer window that fill up but never go away.) I have both a TextBox and a TextBlock in my code just for testing. It runs fine if I comment out the TextBox and leave only the TextBlock, so I know the DataContext is getting set and the binding to PatternName does work. There are no errors in the Output window to help me debug. Please help! I've spent hours on this problem. What can possible be happening?
<StackPanel x:Name="HolePatternStackPanel" >
<TextBlock Text="{Binding PatternName}" Width="75" />
<TextBox Text="{Binding PatternName}" Height="25" Width="125"/>
</StackPanel>
Here is the code that sets the DataContext from a calling ListBox.SelectionChanged method:
private void lvHolePatterns_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
HolePatternStackPanel.DataContext = this.ActivePattern;
}
Well, I've learned more about this... This whole thing is a Master-Detail UI design, and so I had my ListBox using SelectedItem="{Binding ActivePattern}", and apparently, some infinite loop was getting set up between that and the SelectionChanged eventhandler.
So now my question now becomes what good is SelectedItem anyway? Since I had to add a SelectionChanged eventhandler to update the DataContext of the detail stack panel?
You wouldn't need to use the SelectionChanged event if you set the DataContext of the controls with the SelectedItem
for example
<Grid DataContext="{Binding SelectedItem}">
<TextBlock Text="{Binding some_field_in_selecteditem}" />
</Grid>