I have a html in my property Description. When I bind this property with a textblock (ex texthide), it shows the html in textblock. But I can't bind the WebBrowser with this property. How can I bind html string to WebBrowser?
<ScrollViewer
HorizontalScrollBarVisibility="Disabled"
VerticalScrollBarVisibility="Auto"
Margin="25, 0, 0, 0"
Grid.Row="0">
<StackPanel Orientation="Vertical">
<TextBlock
x:Name="TextHide"
Text="{Binding Path=Descrption}"
Style="{StaticResource servicesText}"
TextWrapping="Wrap" />
<phone:WebBrowser
Source="{Binding Descrption}"
x:Name="webBrowserHTML"
Foreground="Black"
Loaded="webBrowserHTML_Loaded" />
<!--<Image Source="../Images/cont_banner.png" Width="270" Grid.Row="1"/>-->
<Button Grid.Row="1">
<Button.Background>
<ImageBrush ImageSource="../Images/cont_banner.png" />
</Button.Background>
<Button.Content>
<HyperlinkButton Content="" NavigateUri="callto:3950" />
</Button.Content>
</Button>
</StackPanel>
</ScrollViewer>
Any ideas please?
Best regards
In order to be able to bind HTML directly to the WebBrowser control, you have to create an attached property:
namespace YourAppNamespace
{
public static class WebBrowserHelper
{
public static readonly DependencyProperty HtmlProperty = DependencyProperty.RegisterAttached(
"Html", typeof(string), typeof(WebBrowserHelper), new PropertyMetadata(OnHtmlChanged));
public static string GetHtml(DependencyObject dependencyObject)
{
return (string)dependencyObject.GetValue(HtmlProperty);
}
public static void SetHtml(DependencyObject dependencyObject, string value)
{
dependencyObject.SetValue(HtmlProperty, value);
}
private static void OnHtmlChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var browser = d as WebBrowser;
if (browser == null)
return;
var html = e.NewValue.ToString();
browser.NavigateToString(html);
}
}
}
Add the required XAML Namespace declaration:
xmlns:cxi="clr-namespace:YourAppNamespace"
And use it like this:
<phone:WebBrowser cxi:WebBrowserHelper.Html="{Binding Question.Body}" />
Source
Related
I have TreeView with binded collection. How can I get object from TreeViewItem Context Flyout with Button in this Flyout by Click event? Can't use selected item because right clicking don't set item as selected. Thanks, I'm new with WinUI 3. Here's my code:
<DataTemplate x:Key="TreeViewPalleteItemTemplate" x:DataType="local:PalleteItem">
<TreeViewItem>
<TreeViewItem.ContextFlyout>
<Flyout Placement="BottomEdgeAlignedLeft">
<StackPanel Orientation="Horizontal">
<Button Width="60" Height="60" Click="DeletePalleteItem_Click">
<SymbolIcon Symbol="Delete"/>
</Button>
<Button Width="60" Height="60">
<SymbolIcon Symbol="Setting"/>
</Button>
</StackPanel>
</Flyout>
</TreeViewItem.ContextFlyout>
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<Image Source="Graphics/part.png" Margin="0,0,5,0" Stretch="Uniform" MaxHeight="20"/>
<TextBlock Text="{Binding Item.DrawingNumber}" FontSize="18" />
<TextBlock Text=" - " FontSize="18" />
<TextBlock Text="{Binding Item.DrawingName}" FontSize="18" />
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="30,0,0,0">
<TextBlock Text="{Binding EmployeeName}" FontSize="16"/>
<TextBlock Text=" - (" FontSize="16"/>
<TextBlock Text="{Binding CountString}" FontSize="16"/>
<TextBlock Text=")" FontSize="16"/>
</StackPanel>
</StackPanel>
</TreeViewItem>
</DataTemplate>
private void DeletePalleteItem_Click(object sender, RoutedEventArgs e)
{
here get clicked object (PalleteItem).....
}
OK. finally I get solution by adding RightTapped event and set TreeViewItem as selected. But is there another (better) solution to get object ?
private void TreeViewItem_RightTapped(object sender, RightTappedRoutedEventArgs e)
{
if (sender is TreeViewItem item)
{
item.IsSelected = true;
}
}
This is another option:
<Grid>
<TreeView
x:Name="TreeViewControl"
ItemsSource="{x:Bind Items}">
<TreeView.ItemTemplate>
<DataTemplate x:DataType="local:Item">
<TreeViewItem Content="{x:Bind Id}">
<TreeViewItem.ContextFlyout>
<Flyout>
<Button
Click="Button_Click"
Content="Click" />
</Flyout>
</TreeViewItem.ContextFlyout>
</TreeViewItem>
</DataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</Grid>
public class Item
{
public string Id { get; set; } = string.Empty;
}
public sealed partial class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();
}
public ObservableCollection<Item> Items { get; } = new()
{
new Item() { Id = "A" },
new Item() { Id = "B" },
new Item() { Id = "C" },
};
private void Button_Click(object sender, RoutedEventArgs e)
{
if (sender is Button button &&
button.DataContext is Item item)
{
// Do something here...
}
}
}
is there any chance to put badge count to the AppBarButton control on Windows 10, as you can do it on Android - e.g. How to display count of notifications in app launcher icon
If not, any idea what is a good practice to inform user about some new information (like new message)?
thanks!
http://i.stack.imgur.com/WjHNt.png
If you just want to create a control in an UWP app like your second picture, you can do it like below:
<Button Background="Transparent" Click="OnClick" HorizontalAlignment="Center">
<RelativePanel>
<TextBlock FontFamily="Segoe MDL2 Assets" FontSize="30" Text=""/>
<Border Background="Red" RelativePanel.AlignBottomWithPanel="True" RelativePanel.AlignRightWithPanel="True">
<TextBlock x:Name="count" Foreground="White" Text="{x:Bind tb.Text,Mode=OneWay}" FontSize="12" />
</Border>
</RelativePanel>
</Button>
<TextBox VerticalAlignment="Bottom" x:Name="tb" />
In this sample, I bind the Text of the TextBlock named "count" to a TextBox, so we can change this value.
But we can create it as an UserControl, so that we can use it repeatable. And we can use a DependencyProperty to expose the property we want to use. For example here:
<UserControl
x:Class="BadgeCountApp.MyUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:BadgeCountApp"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Button Background="Transparent" HorizontalAlignment="Center">
<RelativePanel>
<TextBlock FontFamily="Segoe MDL2 Assets" FontSize="30" x:Name="text" Text="{x:Bind SymbText,Mode=OneWay}"/>
<Border Background="Red" RelativePanel.AlignBottomWithPanel="True" RelativePanel.AlignRightWithPanel="True">
<TextBlock x:Name="count" Foreground="White" FontSize="12" Text="{x:Bind SymbCount,Mode=OneWay}" Visibility="Collapsed"/>
</Border>
</RelativePanel>
</Button>
</UserControl>
code behind:
public sealed partial class MyUserControl : UserControl
{
public MyUserControl()
{
this.InitializeComponent();
}
public static readonly DependencyProperty SymbTextProperty = DependencyProperty.Register("SymbText", typeof(string), typeof(MyUserControl), new PropertyMetadata(string.Empty));
public static readonly DependencyProperty SymbCountProperty = DependencyProperty.Register("SymbCount", typeof(int), typeof(MyUserControl), new PropertyMetadata(0, new PropertyChangedCallback(ChangedCallback)));
private static void ChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
MyUserControl muc = (MyUserControl)d;
int value = (int)e.NewValue;
if (value == 0)
{
muc.count.Visibility = Visibility.Collapsed;
}
else
{
muc.count.Visibility = Visibility.Visible;
}
}
public string SymbText
{
get { return (string)GetValue(SymbTextProperty); }
set { SetValue(SymbTextProperty, value); }
}
public int SymbCount
{
get{ return (int)GetValue(SymbCountProperty);}
set{ SetValue(SymbCountProperty, value);}
}
}
Now you can use this control directly in other place like this:
<local:MyUserControl x:Name="user" SymbText="" Tapped="OnTapped"/>
code behind:
private void OnTapped(object sender, TappedRoutedEventArgs e)
{
if (user.SymbCount == 0)
user.SymbCount = 3;
else
user.SymbCount = 0;
}
The following is a screenshot of this UserControl:
I think the question says it all: I want to build an Windows 8 Store app but how can I build an UI like this:
I think left is a normal ListView. The important thing is that I want an own AppBar for each ListViewEntry. And how can I load different pages (with different AppBars) in the right content area?
You can put a ContentControl in the main panel and another one in the AppBar, then bind their Content properties to the SelectedItem property of the ListBox on the left and use ContentTemplateSelector to display the right UI for the selected page.
*EDIT - Sample
XAML
<Page
x:Class="App17.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App17"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.Resources>
<local:PropertyPageTemplateSelector
x:Key="PropertyPageTemplateSelector"/>
</Page.Resources>
<Grid
Background="LemonChiffon">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition
Width="2*" />
</Grid.ColumnDefinitions>
<ListBox
x:Name="listBox"
DisplayMemberPath="Label"/>
<ContentControl
Grid.Column="1"
Content="{Binding SelectedItem, ElementName=listBox}"
ContentTemplateSelector="{StaticResource PropertyPageTemplateSelector}">
<ContentControl.Resources>
<DataTemplate
x:Key="Options">
<StackPanel>
<TextBlock
Text="{Binding Label}"
FontSize="32"
Foreground="SlateGray" />
<Rectangle
Width="400"
Height="400"
Fill="Aquamarine" />
</StackPanel>
</DataTemplate>
<DataTemplate
x:Key="Preferences">
<StackPanel>
<TextBlock
Text="{Binding Label}"
FontSize="32"
Foreground="SlateGray" />
<Rectangle
Width="400"
Height="400"
Fill="Khaki" />
</StackPanel>
</DataTemplate>
<DataTemplate
x:Key="Properties">
<StackPanel>
<TextBlock
Text="{Binding Label}"
FontSize="32"
Foreground="SlateGray" />
<Rectangle
Width="400"
Height="400"
Fill="LawnGreen" />
</StackPanel>
</DataTemplate>
<DataTemplate
x:Key="Settings">
<StackPanel>
<TextBlock
Text="{Binding Label}"
FontSize="32"
Foreground="SlateGray" />
<Rectangle
Width="400"
Height="400"
Fill="Violet" />
</StackPanel>
</DataTemplate>
</ContentControl.Resources>
</ContentControl>
</Grid>
</Page>
C#
using System.Collections.Generic;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace App17
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
listBox.ItemsSource =
new List<PropertyPageViewModel>(
new PropertyPageViewModel[]
{
new OptionsPropertyPageViewModel(),
new PreferencesPropertyPageViewModel(),
new PropertiesPropertyPageViewModel(),
new SettingsPropertyPageViewModel(),
});
}
}
public abstract class PropertyPageViewModel
{
public abstract string Label { get; }
}
public class OptionsPropertyPageViewModel : PropertyPageViewModel
{
public override string Label { get { return "Options"; } }
}
public class PreferencesPropertyPageViewModel : PropertyPageViewModel
{
public override string Label { get { return "Preferences"; } }
}
public class PropertiesPropertyPageViewModel : PropertyPageViewModel
{
public override string Label { get { return "Properties"; } }
}
public class SettingsPropertyPageViewModel : PropertyPageViewModel
{
public override string Label { get { return "Settings"; } }
}
public class PropertyPageTemplateSelector : DataTemplateSelector
{
protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
{
if (item == null) return base.SelectTemplateCore(item, container);
var contentControl = (ContentControl)container;
var viewModel = (PropertyPageViewModel)item;
var templateKey = viewModel.Label;
return (DataTemplate)contentControl.Resources[templateKey];
}
}
}
So, I have a code:
<tool:LongListSelector x:Name="citiesListGropus" Background="Transparent"
ItemTemplate="{StaticResource citiesItemTemplate}"
GroupHeaderTemplate="{StaticResource groupHeaderTemplate}"
GroupItemTemplate="{StaticResource groupItemTemplate}" Height="468" Margin="0,68,0,0">
<tool:LongListSelector.GroupItemsPanel>
<ItemsPanelTemplate>
<tool:WrapPanel/>
</ItemsPanelTemplate>
</tool:LongListSelector.GroupItemsPanel>
</tool:LongListSelector>
And have a DataTemplate:
<DataTemplate x:Key="groupHeaderTemplate" x:Name="groupHeaderTemplateName">
<Border Background="{StaticResource PhoneAccentBrush}" Width="75" Height="75" Margin="-333, 15, 0, 15" x:Name="groupHeaderName">
<TextBlock Text="{Binding Title}" FontSize="40" Foreground="White" Margin="15, 15, 0, 0"/>
</Border>
</DataTemplate>
How can I change a property Visibility of Border in DataTemplate ? I want to hide DataTemplate. But I can't bind my data to a property such as TextBlock Text="{Binding Title}", because I make binding data once, then I need to change some properties after data binding.
Any Ideas?
UPDATE 1
So, my main goal is filter the list of cities. I want to filter them by name.
My algorithm:
1) I get data from WebService.
2) Load my ViewModel
if (!App.CitiesViewModel.IsDataLoaded)
{
App.CitiesViewModel.LoadData(serviceResponse.Result);
}
3) Group the cities by name. My code like this code.
This is important, especially for LongListSeletstor I have to use templates.
Ok, my data ready. Now I do not need a web service.
<!-- The template for city items -->
<DataTemplate x:Key="citiesItemTemplate">
<StackPanel Name="PlacePanel"
Orientation="Horizontal" Margin="0,0,0,17"
Tag="{Binding Id}" Visibility="{Binding IsVisibility}">
<Image Height="75" Width="75" HorizontalAlignment="Left" Margin="12,0,9,0" Name="Image" Stretch="Fill" VerticalAlignment="Top" Source="{Binding Image}"/>
<StackPanel Width="311">
<TextBlock Text="{Binding Name}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
</StackPanel>
</StackPanel>
</DataTemplate>
I have TextBox, when I type name of a city - unnecessary cities hide. I can use data binding and INotifyPropertyChanged, so I can hide cities.
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
TextBox name = (TextBox)sender;
foreach(var place in App.CitiesViewModel.Items
.Where(city => (city.Name.Contains(name.Text)) && (city.IsShow == true)))
{
city.IsVisibility = Visibility.Visible;
}
foreach (var city in App.CitiesViewModel.Items
.Where(city => !city.Name.Contains(name.Text)))
{
city.IsVisibility = Visibility.Collapsed;
}
}
But I have some problems, I can't hide this DataTemplate when I search cities:
<DataTemplate x:Key="groupHeaderTemplate" x:Name="groupHeaderTemplateName">
<Border Background="{StaticResource PhoneAccentBrush}" Width="75" Height="75" Margin="-333, 15, 0, 15" x:Name="groupHeaderName">
<TextBlock Text="{Binding Title}" FontSize="40" Foreground="White" Margin="15, 15, 0, 0"/>
</Border>
</DataTemplate>
When I type name of a cities I want hide GroupHeaderTemplate. When TextBox lost focus show GroupHeaderTemplate.
I borrowed this picture here.
Update 2
My mistake was that I did not use PropertyChangedEventHandler. After adding this to GroupCity property and implementing an interface INotifyPropertyChanged I can change property Data Template after data binding.
private IList<Group<City>> citiesCollectionGroup;
var cityByName = from city in App.ViewModel.Items
group city by Convert.ToString(City.Name[0]).ToLower()
into c orderby c.Key select new
GroupCity<CitiesViewModel>(Convert.ToString(c.Key[0]).ToLower(), c);
citiesCollectionGroup = cityByName.ToList();
this.citiesListGropus.ItemsSource = citiesCollectionGroup;
public class GroupCity<T> : IEnumerable<T>, INotifyPropertyChanged
{
public GroupCity(string name, IEnumerable<T> items)
{
this.Title = name;
this.IsVisibility = Visibility.Visible;
this.Items = new List<T>(items);
}
public override bool Equals(object obj)
{
GroupCity<T> that = obj as GroupCity<T>;
return (that != null) && (this.Title.Equals(that.Title));
}
public string Title
{
get;
set;
}
private Visibility _isVisibility;
public Visibility IsVisibility
{
get
{
return _isVisibility;
}
set
{
_isVisibility = value;
NotifyPropertyChanged("IsVisibility");
}
}
public IList<T> Items
{
get;
set;
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
#region IEnumerable<T> Members
public IEnumerator<T> GetEnumerator()
{
return this.Items.GetEnumerator();
}
#endregion
#region IEnumerable Members
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.Items.GetEnumerator();
}
#endregion
}
Xaml code:
<DataTemplate x:Key="groupHeaderTemplate" x:Name="groupHeaderTemplateName">
<Border Background="{StaticResource PhoneAccentBrush}" Width="75" Height="75" Margin="-333, 15, 0, 15" x:Name="groupHeaderName" Visibility="{Binding IsVisibility}">
<TextBlock Text="{Binding Title}" FontSize="40" Foreground="White" Margin="15, 15, 0, 0"/>
</Border>
</DataTemplate>
Now we can change Visibility groupHeaderTemplate.
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
foreach (GroupCity<CitiesViewModel> city in citiesCollectionGroup)
{
city.IsVisibility = Visibility.Collapsed;
}
//
}
private void SearchText_LostFocus(object sender, RoutedEventArgs e)
{
foreach (GroupCity<CitiesViewModel> city in citiesCollectionGroup)
{
city.IsVisibility = Visibility.Visible;
}
//
}
You can write your own simple DataTemplateSelector to do this. Check this out for more information.
With "How can I change a property Visibility of Border in DataTemplate ? I want to hide DataTemplate. " I understand that you want to change the visibility of an item based on a property, see example below.
(If the code below isn't quite the solution you are looking for please provide some more information about what you are trying to achieve. Re-reading your question and the comments I got on this example I doubt this is what you want to do, but I don't want to guess so I'm simply answering my interpretation of your question).
With bindings you can:
Bind the visibility property directly to a a property of type Visibility (example with listbox and Person1)
Bind the Visibility property to a boolean property, and use a converter to convert that to the Visibility type (listbox2 and Person2 + BoolToVis class).
There are more ways, but this are the two most simple to implement in my opinion.¨
result of code below:
View:
<phone:PhoneApplicationPage
x:Class="VisibilityWP.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
xmlns:VisibilityWP="clr-namespace:VisibilityWP"
shell:SystemTray.IsVisible="True">
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Grid.Resources>
<VisibilityWP:BoolToVis x:Key="BooleanToVisibilityConverter" />
</Grid.Resources>
<StackPanel>
<ListBox x:Name="listBox" Height="300">
<ListBox.ItemTemplate>
<DataTemplate>
<Border Width="200" Visibility="{Binding Visibility}" Background="Blue">
<TextBlock Text="{Binding Name}" FontSize="40" Foreground="White" Margin="15, 15, 0, 0"/>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<ListBox x:Name="listBox2" Height="300">
<ListBox.ItemTemplate>
<DataTemplate>
<Border Width="200" Visibility="{Binding ShowBorder, Converter={StaticResource BooleanToVisibilityConverter}}" Background="Red">
<TextBlock Text="{Binding Name}" FontSize="40" Foreground="White" Margin="15, 15, 0, 0"/>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</Grid>
</Grid>
Code behind:
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Data;
namespace VisibilityWP
{
public partial class MainPage
{
public MainPage()
{
InitializeComponent();
DataContext = this;
listBox.ItemsSource = new List<Person1>
{
new Person1 {Name = "Iris", Visibility = Visibility.Visible},
new Person1 {Name = "Somebody", Visibility = Visibility.Collapsed},
new Person1 {Name = "Peter", Visibility = Visibility.Visible},
};
listBox2.ItemsSource = new List<Person2>
{
new Person2 {Name = "Iris", ShowBorder = true},
new Person2 {Name = "Mia", ShowBorder = true},
new Person2 {Name = "Somebody", ShowBorder = false}
};
}
}
public class Person1
{
public string Name { get; set; }
public Visibility Visibility { get; set; }
}
public class Person2
{
public string Name { get; set; }
public bool ShowBorder { get; set; }
}
public sealed class BoolToVis : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return (value is bool && (bool)value) ? Visibility.Visible : Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value is Visibility && (Visibility)value == Visibility.Visible;
}
}
}
So, I found a way of creating RSS feed app for Windows Phone. It binds SyndicationFeed items into Listbox template. And it works great!
However, I wanted to make an app, that handles a bit different. I thought, that changing Listbox template into Pivot would be quite easy. The problem is, that instead of articles I get only "System.ServiceModel.Syndication.SyndicationItem".
I am out of ideas how to fix that, could anyone help me?
Xaml:
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
d:DataContext="{d:DesignData SampleData/MainViewModelSampleData.xaml}"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Popup Name="myPopup" IsOpen="True" Width="Auto">
<ProgressBar Height="Auto" IsIndeterminate="True" Width="480" />
</Popup>
<controls:Pivot Name="FeedPivot" Loaded="loadFeed">
<controls:Pivot.Title>
<TextBlock FontSize="56"> Komorkomania </TextBlock>
</controls:Pivot.Title>
<controls:Pivot.HeaderTemplate>
<DataTemplate>
<Grid>
<TextBlock>test1</TextBlock>
</Grid>
</DataTemplate>
</controls:Pivot.HeaderTemplate>
<controls:Pivot.DataContext>
<DataTemplate>
<StackPanel>
<TextBlock>test</TextBlock>
<!--
<TextBlock FontWeight="Bold" FontSize="28" Name="feedTitle" TextWrapping="Wrap" Margin="12,0,0,0" HorizontalAlignment="Left" Foreground="{StaticResource PhoneAccentBrush}" Text="{Binding Title.Text, Converter={StaticResource RssTextTrimmer}}" Width="430" />
<TextBlock Name="feedSummary" FontSize="24" TextWrapping="Wrap" Margin="12,0,0,0" Text="{Binding Summary.Text, Converter={StaticResource RssTextTrimmer}}" Width="430" />
<TextBlock Name="feedPubDate" Foreground="{StaticResource PhoneSubtleBrush}" Margin="12,0,0,10" Text="{Binding PublishDate.DateTime}" Width="430" />
-->
</StackPanel>
</DataTemplate>
</controls:Pivot.DataContext>
</controls:Pivot>
</Grid>
Code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO;
using System.ServiceModel.Syndication;
using System.Xml;
using Microsoft.Phone.Tasks;
using Microsoft.Phone.Shell;
using System.ComponentModel;
using System.Windows.Controls.Primitives;
namespace KomorkomaniaRss
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
myPopup.IsOpen = true;
}
// Co robimy jak już w końcu się ściągnie?
private void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null)
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
MessageBox.Show(e.Error.Message);
});
}
else
{
// Save the feed into the State property in case the application is tombstoned.
this.State["feed"] = e.Result;
UpdateFeedList(e.Result);
myPopup.IsOpen = false;
}
}
// This method determines whether the user has navigated to the application after the application was tombstoned.
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
// First, check whether the feed is already saved in the page state.
if (this.State.ContainsKey("feed"))
{
// Get the feed again only if the application was tombstoned, which means the ListBox will be empty.
// This is because the OnNavigatedTo method is also called when navigating between pages in your application.
// You would want to rebind only if your application was tombstoned and page state has been lost.
if (FeedPivot.Items.Count == 0)
{
UpdateFeedList(State["feed"] as string);
}
}
}
// This method sets up the feed and binds it to our ListBox.
private void UpdateFeedList(string feedXML)
{
// Load the feed into a SyndicationFeed instance.
StringReader stringReader = new StringReader(feedXML);
XmlReader xmlReader = XmlReader.Create(stringReader);
SyndicationFeed feed = SyndicationFeed.Load(xmlReader);
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
// Bind the list of SyndicationItems to our ListBox.
FeedPivot.ItemsSource = feed.Items;
});
}
public void feedLoader()
{
myPopup.IsOpen = true;
WebClient webClient = new WebClient();
webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
webClient.DownloadStringAsync(new System.Uri("http://komorkomania.pl/feed/rss2"));
}
private void runBrowser(object sender)
{
ListBox listBox = sender as ListBox;
if (listBox != null && listBox.SelectedItem != null)
{
// Get the SyndicationItem that was tapped.
SyndicationItem sItem = (SyndicationItem)listBox.SelectedItem;
// Set up the page navigation only if a link actually exists in the feed item.
if (sItem.Links.Count > 0)
{
// Get the associated URI of the feed item.
Uri uri = sItem.Links.FirstOrDefault().Uri;
// Create a new WebBrowserTask Launcher to navigate to the feed item.
// An alternative solution would be to use a WebBrowser control, but WebBrowserTask is simpler to use.
WebBrowserTask webBrowserTask = new WebBrowserTask();
webBrowserTask.Uri = uri;
webBrowserTask.Show();
}
}
}
// The SelectionChanged handler for the feed items
private void feedListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
runBrowser(sender);
}
private void loadFeed(object sender, RoutedEventArgs e)
{
feedLoader();
if (!App.ViewModel.IsDataLoaded)
{
App.ViewModel.LoadData();
}
}
}
}
There are some "leftovers" for handling another actions. Please ignore them, I didn't get to it yet. The biggest focus is to make it show text after app is loaded.
I bealive that I fixed that. There was a problem with Pivot concept. This is how I fixed that:
<controls:Pivot Name="FeedPivot" Loaded="loadFeed" ScrollViewer.VerticalScrollBarVisibility="Visible" Tap="feedPivotTap" Margin="0,88,0,0" LoadedPivotItem="getPivotItem">
<controls:Pivot.HeaderTemplate>
<DataTemplate>
<TextBlock FontWeight="Bold" FontSize="28" Name="feedTitle" TextWrapping="Wrap" Margin="12,0,0,0" HorizontalAlignment="Left" Foreground="#FF5DBA00" Text="{Binding Title.Text, Converter={StaticResource RssTextTrimmer}}" Width="430" />
</DataTemplate>
</controls:Pivot.HeaderTemplate>
<controls:Pivot.ItemTemplate>
<DataTemplate>
<ScrollViewer>
<StackPanel>
<TextBlock Name="feedSummary" FontSize="24" TextWrapping="Wrap" Margin="12,0,0,0" Text="{Binding Summary.Text, Converter={StaticResource RssTextTrimmer}}" Width="430" />
<TextBlock Name="feedPubDate" Foreground="{StaticResource PhoneSubtleBrush}" Margin="12,0,0,10" Text="{Binding PublishDate.DateTime}" Width="430" />
</StackPanel>
</ScrollViewer>
</DataTemplate>
</controls:Pivot.ItemTemplate>
</controls:Pivot>