WPF not showing one collection - c#

i have two observable Collections that my view is binded to, one is called Network.Nodes and one it Network.Connections - the Network object is a Property in my view model.
I'm getting data from BackgroundWorker and adding new Node\Connection via the BW.ProgressChanged.
When the BW finish it's work i see only nodes and no connection. i added a Debug.WriteLine("Network.Connection.Cout = " + Network.Connection.Cout);
On the BW finish work, and i see that there are 2 elements inside (which is the correct amount) but for some reason i don't see the elements.
The ProgressChanged:
if (e.ProgressPercentage == 2) //Add connection to UI
{
ConnectionViewModel tempCon = new ConnectionViewModel();
List<Object> ConnectionObj = new List<object>();
ConnectionObj = (List<Object>)e.UserState;
tempCon.SourceConnector = (ConnectorViewModel)ConnectionObj[0];
tempCon.DestConnector = (ConnectorViewModel)ConnectionObj[1];
Network.Connections.Add(tempCon);
This is how i call the ProgressChange:
List<Object> connectionObj = new List<Object>();
connectionObj.Add(connection.SourceConnector);
connectionObj.Add(connection.DestConnector);
connectionObj.Add(connection.Type);
connectionObj.Add(i++);
bw.ReportProgress(2, connectionObj);
The binding:
NodesSource="{Binding Network.Nodes}"
ConnectionsSource="{Binding Network.Connections}"
The Network in the View Model:
public NetworkViewModel Network
{
get
{
return network;
}
set
{
network = value;
OnPropertyChanged("Network");
}
}
The Network View Model has two observableCollections one is of type NodeView model and one is ConnectionViewModel.
the Connection View model:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utils;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Media;
using System.Windows;
namespace YogevAfekaRAPAT.YNIDS.ViewModels
{
/// <summary>
/// Defines a connection between two connectors (aka connection points) of two nodes.
/// </summary>
public sealed class ConnectionViewModel : AbstractModelBase
{
#region Internal Data Members
/// <summary>
/// The source connector the connection is attached to.
/// </summary>
private ConnectorViewModel sourceConnector = null;
/// <summary>
/// The destination connector the connection is attached to.
/// </summary>
private ConnectorViewModel destConnector = null;
/// <summary>
/// The source and dest hotspots used for generating connection points.
/// </summary>
private Point sourceConnectorHotspot;
private Point destConnectorHotspot;
/// <summary>
/// Points that make up the connection.
/// </summary>
private PointCollection points = null;
#endregion Internal Data Members
public enum ConnectorType
{
REGULAR = 0,
FLOW = 1
}
/// <summary>
/// The source connector the connection is attached to.
/// </summary>
public ConnectorViewModel SourceConnector
{
get
{
return sourceConnector;
}
set
{
if (sourceConnector == value)
{
return;
}
if (sourceConnector != null)
{
sourceConnector.AttachedConnections.Remove(this);
sourceConnector.HotspotUpdated -= new EventHandler<EventArgs>(sourceConnector_HotspotUpdated);
}
sourceConnector = value;
if (sourceConnector != null)
{
sourceConnector.AttachedConnections.Add(this);
sourceConnector.HotspotUpdated += new EventHandler<EventArgs>(sourceConnector_HotspotUpdated);
this.SourceConnectorHotspot = sourceConnector.Hotspot;
}
OnPropertyChanged("SourceConnector");
OnConnectionChanged();
}
}
/// <summary>
/// The destination connector the connection is attached to.
/// </summary>
public ConnectorViewModel DestConnector
{
get
{
return destConnector;
}
set
{
if (destConnector == value)
{
return;
}
if (destConnector != null)
{
destConnector.AttachedConnections.Remove(this);
destConnector.HotspotUpdated -= new EventHandler<EventArgs>(destConnector_HotspotUpdated);
}
destConnector = value;
if (destConnector != null)
{
destConnector.AttachedConnections.Add(this);
destConnector.HotspotUpdated += new EventHandler<EventArgs>(destConnector_HotspotUpdated);
this.DestConnectorHotspot = destConnector.Hotspot;
}
OnPropertyChanged("DestConnector");
OnConnectionChanged();
}
}
/// <summary>
/// The source and dest hotspots used for generating connection points.
/// </summary>
public Point SourceConnectorHotspot
{
get
{
return sourceConnectorHotspot;
}
set
{
sourceConnectorHotspot = value;
ComputeConnectionPoints();
OnPropertyChanged("SourceConnectorHotspot");
}
}
public Point DestConnectorHotspot
{
get
{
return destConnectorHotspot;
}
set
{
destConnectorHotspot = value;
ComputeConnectionPoints();
OnPropertyChanged("DestConnectorHotspot");
}
}
/// <summary>
/// Points that make up the connection.
/// </summary>
public PointCollection Points
{
get
{
return points;
}
set
{
points = value;
OnPropertyChanged("Points");
}
}
private ConnectorType type;
public ConnectorType Type
{
get
{
return type;
}
set
{
type = value;
OnPropertyChanged("Type");
}
}
/// <summary>
/// Event fired when the connection has changed.
/// </summary>
public event EventHandler<EventArgs> ConnectionChanged;
#region Private Methods
/// <summary>
/// Raises the 'ConnectionChanged' event.
/// </summary>
private void OnConnectionChanged()
{
if (ConnectionChanged != null)
{
ConnectionChanged(this, EventArgs.Empty);
}
}
#endregion Private Methods
}
}

I am not sure, but it might be that you're trying to update the collections from the background worker, which can result in problems. You need to marshal that to the UI-Thread using a Dispatcher like:
view.Dispatcher.BeginInvoke(new Action(() =>
{
Network.Connections.Add(tempCon);
}), DispatcherPriority.Normal);
A Dispatcher can be found on all UI-Elements. Not sure how your code is structured so can't tell where to get one from without seeing more code or getting more information.

Related

How to hide all the icons in the media player other than Play/Pause and Volume button?

I have a media player in which I want to hide the aspect ratio, cast to device and full screen icon in media player. This is the screenshot of my media player in which I have marked the icon which I need to hide in red color.
So I have taken this media player from this link. This is the my Xaml code
<StackLayout>
<Button Text="Stretch" Clicked="Button_Clicked"/>
<forms1:MediaElement HorizontalOptions="Fill" BackgroundColor="Green" VerticalOptions="Center" HeightRequest="180" x:Name="Media" IsLooping="True" AreTransportControlsEnabled="true" Source="http://video.ch9.ms/ch9/334f/891b78a5-642d-40b4-8d02-ff40ffdd334f/LoginToLinkedinUSingXamarinAuth_mid.mp4"/>
</StackLayout>
This is my media element code-
public sealed class MediaElement : View
{
/// <summary>
/// Identifies the AreTransportControlsEnabled dependency property.
/// </summary>
public static readonly BindableProperty AreTransportControlsEnabledProperty =
BindableProperty.Create(nameof(AreTransportControlsEnabled), typeof(bool), typeof(MediaElement), false);
/// <summary>
/// Identifies the AutoPlay dependency property.
/// </summary>
public static readonly BindableProperty AutoPlayProperty =
BindableProperty.Create(nameof(AutoPlay), typeof(bool), typeof(MediaElement), true);
/// <summary>
/// Identifies the BufferingProgress dependency property.
/// </summary>
public static readonly BindableProperty BufferingProgressProperty =
BindableProperty.Create(nameof(BufferingProgress), typeof(double), typeof(MediaElement), 0.0);
/// <summary>
/// Identifies the IsLooping dependency property.
/// </summary>
public static readonly BindableProperty IsLoopingProperty =
BindableProperty.Create(nameof(IsLooping), typeof(bool), typeof(MediaElement), false);
/// <summary>
/// Identifies the KeepScreenOn dependency property.
/// </summary>
public static readonly BindableProperty KeepScreenOnProperty =
BindableProperty.Create(nameof(KeepScreenOn), typeof(bool), typeof(MediaElement), false);
/// <summary>
/// Identifies the Source dependency property.
/// </summary>
public static readonly BindableProperty SourceProperty =
BindableProperty.Create(nameof(Source), typeof(Uri), typeof(MediaElement));
/// <summary>
/// Identifies the CurrentState dependency property.
/// </summary>
public static readonly BindableProperty CurrentStateProperty =
BindableProperty.Create(nameof(CurrentState), typeof(MediaElementState), typeof(MediaElement), MediaElementState.Closed);
/// <summary>
/// Identifies the Position dependency property.
/// </summary>
public static readonly BindableProperty PositionProperty =
BindableProperty.Create(nameof(Position), typeof(TimeSpan), typeof(MediaElement), TimeSpan.Zero, validateValue: ValidatePosition);
private static bool ValidatePosition(BindableObject bindable, object value)
{
MediaElement element = bindable as MediaElement;
if (element != null)
{
if (element._renderer != null)
{
element._renderer.Seek((TimeSpan)value);
}
}
return true;
}
/// <summary>
/// Identifies the Stretch dependency property.
/// </summary>
public static readonly BindableProperty StretchProperty =
BindableProperty.Create(nameof(Stretch), typeof(Stretch), typeof(MediaElement), Stretch.Uniform);
private IMediaElementRenderer _renderer = null;
public void SetRenderer(IMediaElementRenderer renderer)
{
_renderer = renderer;
}
/// <summary>
/// Gets or sets a value that determines whether the standard transport controls are enabled.
/// </summary>
public bool AreTransportControlsEnabled
{
get { return (bool)GetValue(AreTransportControlsEnabledProperty); }
set { SetValue(AreTransportControlsEnabledProperty, value); }
}
/// <summary>
/// Gets or sets a value that indicates whether media will begin playback automatically when the <see cref="Source"/> property is set.
/// </summary>
public bool AutoPlay
{
get { return (bool)GetValue(AutoPlayProperty); }
set { SetValue(AutoPlayProperty, value); }
}
/// <summary>
/// Gets a value that indicates the current buffering progress.
/// </summary>
/// <value>The amount of buffering that is completed for media content.
/// The value ranges from 0 to 1.
/// Multiply by 100 to obtain a percentage.</value>
public double BufferingProgress
{
get
{
return (double)GetValue(BufferingProgressProperty);
}
}
/// <summary>
/// Gets or sets a value that describes whether the media source currently loaded in the media engine should automatically set the position to the media start after reaching its end.
/// </summary>
public bool IsLooping
{
get { return (bool)GetValue(IsLoopingProperty); }
set { SetValue(IsLoopingProperty, value); }
}
/// <summary>
/// Gets or sets a value that specifies whether the control should stop the screen from timing out when playing media.
/// </summary>
public bool KeepScreenOn
{
get { return (bool)GetValue(KeepScreenOnProperty); }
set { SetValue(KeepScreenOnProperty, value); }
}
public TimeSpan NaturalDuration
{
get
{
if (_renderer != null)
{
return _renderer.NaturalDuration;
}
return TimeSpan.Zero;
}
}
public int NaturalVideoHeight
{
get
{
if (_renderer != null)
{
return _renderer.NaturalVideoHeight;
}
return 0;
}
}
public int NaturalVideoWidth
{
get
{
if (_renderer != null)
{
return _renderer.NaturalVideoWidth;
}
return 0;
}
}
/// <summary>
/// Gets or sets a media source on the MediaElement.
/// </summary>
[TypeConverter(typeof(Xamarin.Forms.UriTypeConverter))]
public Uri Source
{
get { return (Uri)GetValue(SourceProperty); }
set { SetValue(SourceProperty, value); }
}
private IDictionary<string, string> _httpHeaders = new Dictionary<string, string>();
public IDictionary<string, string> HttpHeaders
{
get
{
return _httpHeaders;
}
}
/// <summary>
/// Gets the status of this MediaElement.
/// </summary>
public MediaElementState CurrentState
{
get { return (MediaElementState)GetValue(CurrentStateProperty); }
internal set
{
SetValue(CurrentStateProperty, value);
}
}
public void RaiseCurrentStateChanged()
{
CurrentStateChanged?.Invoke(this, EventArgs.Empty);
}
/// <summary>
/// Gets or sets the current position of progress through the media's playback time.
/// </summary>
public System.TimeSpan Position
{
get
{
if (_renderer != null)
{
return _renderer.Position;
}
return (TimeSpan)GetValue(PositionProperty);
}
set
{
SetValue(PositionProperty, value);
}
}
/// <summary>
/// Plays media from the current position.
/// </summary>
public void Play()
{
CurrentState = MediaElementState.Playing;
}
/// <summary>
/// Pauses media at the current position.
/// </summary>
public void Pause()
{
if (CurrentState == MediaElementState.Playing)
{
CurrentState = MediaElementState.Paused;
}
}
/// <summary>
/// Stops and resets media to be played from the beginning.
/// </summary>
public void Stop()
{
if (CurrentState != MediaElementState.Stopped)
{
CurrentState = MediaElementState.Stopped;
}
}
/// <summary>
/// Gets or sets a value that describes how an MediaElement should be stretched to fill the destination rectangle.
/// </summary>
/// <value>A value of the <see cref="Stretch"/> enumeration that specifies how the source visual media is rendered.
/// The default value is Uniform.</value>
public Stretch Stretch
{
get
{
return (Stretch)GetValue(StretchProperty);
}
set
{
SetValue(StretchProperty, value);
}
}
/// <summary>
/// Occurs when the value of the <see cref="CurrentState"/> property changes.
/// </summary>
public event EventHandler CurrentStateChanged;
/// <summary>
/// Occurs when the MediaElement finishes playing audio or video.
/// </summary>
public event EventHandler MediaEnded;
public void RaiseMediaOpened()
{
MediaOpened?.Invoke(this, EventArgs.Empty);
}
/// <summary>
/// Occurs when the media stream has been validated and opened, and the file headers have been read.
/// </summary>
public event EventHandler MediaOpened;
public void RaiseSeekCompleted()
{
SeekCompleted?.Invoke(this, EventArgs.Empty);
}
/// <summary>
/// Occurs when the seek point of a requested seek operation is ready for playback.
/// </summary>
public event EventHandler SeekCompleted;
public void OnMediaEnded()
{
CurrentState = MediaElementState.Stopped;
if (MediaEnded != null)
{
System.Diagnostics.Debug.WriteLine("Media Ended");
MediaEnded(this, EventArgs.Empty);
}
}
internal void RaisePropertyChanged(string propertyName)
{
OnPropertyChanged(propertyName);
}
}
public interface IMediaElementRenderer
{
double BufferingProgress { get; }
TimeSpan NaturalDuration { get; }
int NaturalVideoHeight { get; }
int NaturalVideoWidth { get; }
TimeSpan Position { get; }
void Seek(TimeSpan time);
}
}
I don't have any clue how hide those icons. Any suggestions?

Access fields of CheckBoxComboBox

I have a CheckBoxComboBox where I want to get fields of checked items so I do something like:
foreach (var currentChecked in cboDesignStatus.CheckBoxItems.Where(x => x.Checked))
{
var a = currentChecked.ComboBoxItem;
}
So var a result is :
My question is, how can I access this fields ? I want to get property Name so I try
var a = currentChecked.ComboBoxItem.Name;
But I can't it only let this:
How can I access this field
Note: variable on foreach "currentChecked" throws ComboBoxItem as ObjectSelectionWrapper
ObjectSelectionWrapper Class:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Reflection;
using System.Data;
namespace MyProject.Utility
{
/// <summary>
/// Used together with the ListSelectionWrapper in order to wrap data sources for a CheckBoxComboBox.
/// It helps to ensure you don't add an extra "Selected" property to a class that don't really need or want that information.
/// </summary>
public class ObjectSelectionWrapper<T> : INotifyPropertyChanged
{
public ObjectSelectionWrapper(T item, ListSelectionWrapper<T> container)
: base()
{
_Container = container;
_Item = item;
}
#region PRIVATE PROPERTIES
/// <summary>
/// Used as a count indicator for the item. Not necessarily displayed.
/// </summary>
private int _Count = 0;
/// <summary>
/// Is this item selected.
/// </summary>
private bool _Selected = false;
/// <summary>
/// A reference to the wrapped item.
/// </summary>
private T _Item;
/// <summary>
/// The containing list for these selections.
/// </summary>
private ListSelectionWrapper<T> _Container;
#endregion
#region PUBLIC PROPERTIES
/// <summary>
/// An indicator of how many items with the specified status is available for the current filter level.
/// Thaught this would make the app a bit more user-friendly and help not to miss items in Statusses
/// that are not often used.
/// </summary>
public int Count
{
get { return _Count; }
set { _Count = value; }
}
/// <summary>
/// A reference to the item wrapped.
/// </summary>
public T Item
{
get { return _Item; }
set { _Item = value; }
}
/// <summary>
/// The item display value. If ShowCount is true, it displays the "Name [Count]".
/// </summary>
public string Name
{
get
{
string Name = null;
if (string.IsNullOrEmpty(_Container.DisplayNameProperty))
Name = Item.ToString();
else if (Item is DataRow) // A specific implementation for DataRow
Name = ((DataRow)((Object)Item))[_Container.DisplayNameProperty].ToString();
else
{
PropertyDescriptorCollection PDs = TypeDescriptor.GetProperties(Item);
foreach (PropertyDescriptor PD in PDs)
if (PD.Name.CompareTo(_Container.DisplayNameProperty) == 0)
{
Name = (string)PD.GetValue(Item).ToString();
break;
}
if (string.IsNullOrEmpty(Name))
{
PropertyInfo PI = Item.GetType().GetProperty(_Container.DisplayNameProperty);
if (PI == null)
throw new Exception(String.Format(
"Property {0} cannot be found on {1}.",
_Container.DisplayNameProperty,
Item.GetType()));
Name = PI.GetValue(Item, null).ToString();
}
}
return _Container.ShowCounts ? String.Format("{0} [{1}]", Name, Count) : Name;
}
}
/// <summary>
/// The textbox display value. The names concatenated.
/// </summary>
public string NameConcatenated
{
get { return _Container.SelectedNames; }
}
/// <summary>
/// Indicates whether the item is selected.
/// </summary>
public bool Selected
{
get { return _Selected; }
set
{
if (_Selected != value)
{
_Selected = value;
OnPropertyChanged("Selected");
OnPropertyChanged("NameConcatenated");
}
}
}
#endregion
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
}
It looks like ComboBoxItem is an Object. As Nat said earlier, if you cast the object to your expected class, you can then reference the properties.
var itemName = ((Utility.ObjectSelectionWrapper<DataRow>)currentChecked.ComboBoxItem).Name;

PRISM 5 MEF AvalonDock 2.0 DataAdapter Registered Views and Parent IsSelected

I am attempting to build an MVVM Windows Application Using PRISM 5 and I have wrapped my main content window with an AvalonDock (Wrapping Code Below).
using Microsoft.Practices.Prism.Regions;
using Xceed.Wpf.AvalonDock;
using System.Collections.Specialized;
using System.Windows;
using Xceed.Wpf.AvalonDock.Layout;
namespace Central.Adapters
{
using System.Linq;
public class AvalonDockRegionAdapter : RegionAdapterBase<DockingManager>
{
/// <summary>
/// This ties the adapter into the base region factory.
/// </summary>
/// <param name="factory">The factory that determines where the modules will go.</param>
public AvalonDockRegionAdapter(IRegionBehaviorFactory factory)
: base(factory)
{
}
/// <summary>
/// Since PRISM does not support the Avalon DockingManager natively this adapter provides the needed support.
/// </summary>
/// <param name="region">This is the region that resides in the DockingManager.</param>
/// <param name="regionTarget">The DockingManager that needs the window added.</param>
protected override void Adapt(IRegion region, DockingManager regionTarget)
{
region.Views.CollectionChanged += (sender, e) =>
{
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
AddAnchorableDocument(regionTarget, e);
break;
case NotifyCollectionChangedAction.Remove:
break;
}
};
}
/// <summary>
/// This adds the window as an anchorable document to the Avalon DockingManager.
/// </summary>
/// <param name="regionTarget">The DockingManager instance.</param>
/// <param name="e">The new window to be added.</param>
private static void AddAnchorableDocument(DockingManager regionTarget, NotifyCollectionChangedEventArgs e)
{
foreach (FrameworkElement element in e.NewItems)
{
var view = element as UIElement;
var documentPane = regionTarget.Layout.Descendents().OfType<LayoutDocumentPane>().FirstOrDefault();
if ((view == null) || (documentPane == null))
{
continue;
}
var newContentPane = new LayoutAnchorable
{
Content = view,
Title = element.ToolTip.ToString(),
CanHide = true,
CanClose = false
};
documentPane.Children.Add(newContentPane);
}
}
/// <summary>
/// This returns the region instance populated with all of its contents.
/// </summary>
/// <returns>DockingManager formatted region.</returns>
protected override IRegion CreateRegion()
{
return new AllActiveRegion();
}
}
}
I then register this adapter in the bootstrapper this way:
protected override RegionAdapterMappings ConfigureRegionAdapterMappings()
{
var mappings = base.ConfigureRegionAdapterMappings();
if (mappings == null)
{
return null;
}
mappings.RegisterMapping(typeof(DockingManager), new AvalonDockRegionAdapter(ConfigureDefaultRegionBehaviors()));
return mappings;
}
The problem that I am facing is that other region UI elements will require certain LayoutAnchorable windows to become the active and selected window. The content I am feeding into the LayoutAnchorable object is a ContentControl.
In my View's ViewModel I have a property that I am successfully setting using another UI element's interaction. However I am unable to make the connection from ViewModel(Property) -> ContentContro(View) -> LayoutAnchorable(View's Parent).IsSelected or ,IsActive.
I know how to bind to a parent object but that eats up the property and does not allow me to bind it to the ViewModel property as well. I also have no problem binding to a ViewModel property, but that is useless unless I can get it to set the parent property. I have also attempted View based events. Problem with this is that once the view loads it doe not like calling its own events anymore unless it is caused by user interaction directly with that view.
In short I just want to display the appropriate window when needed based on an interaction in another part of my program. Maybe I am making this more complicated than it needs to be. Any assistance on this would be greatly appreciated.
Thanks
James
As I took a break from the problem at had I looked at it from another perspective. To solve the issue I decided to store the instance of the content panes containing the views into a singleton dictionary class:
using System;
using System.Collections.Generic;
using Xceed.Wpf.AvalonDock.Layout;
namespace Central.Services
{
public class DockinWindowChildObjectDictionary
{
private static Dictionary<string, LayoutAnchorable> _contentPane = new Dictionary<string, LayoutAnchorable>();
private static readonly Lazy<DockinWindowChildObjectDictionary> _instance =
new Lazy<DockinWindowChildObjectDictionary>(()=> new DockinWindowChildObjectDictionary(), true);
public static DockinWindowChildObjectDictionary Instance
{
get
{
return _instance.Value;
}
}
/// <summary>
/// Causes the constructor to be private allowing for proper use of the Singleton pattern.
/// </summary>
private DockinWindowChildObjectDictionary()
{
}
/// <summary>
/// Adds a Content Pane instance to the dictionary.
/// </summary>
/// <param name="title">The title given to the Pane during instantiation.</param>
/// <param name="contentPane">The object instance.</param>
public static void Add(string title, LayoutAnchorable contentPane)
{
_contentPane.Add(title, contentPane);
}
/// <summary>
/// If a window needs to be removed from the dock this should be used
/// to also remove it from the dictionary.
/// </summary>
/// <param name="title">The title given to the Pane during instantiation.</param>
public static void Remove(string title)
{
_contentPane.Remove(title);
}
/// <summary>
/// This will return the instance of the content pane that holds the view.
/// </summary>
/// <param name="title">The title given to the Pane during instantiation.</param>
/// <returns>The views Parent Instance.</returns>
public static LayoutAnchorable GetInstance(string title)
{
return _contentPane[title];
}
}
}
In the adapter I modified this code as follows:
private static void AddAnchorableDocument(DockingManager regionTarget, NotifyCollectionChangedEventArgs e)
{
foreach (FrameworkElement element in e.NewItems)
{
var view = element as UIElement;
var documentPane = regionTarget.Layout.Descendents().OfType<LayoutDocumentPane>().FirstOrDefault();
if ((view == null) || (documentPane == null))
{
continue;
}
var newContentPane = new LayoutAnchorable
{
Content = view,
Title = element.ToolTip.ToString(),
CanHide = true,
CanClose = false
};
DockinWindowChildObjectDictionary.Add(element.ToolTip.ToString(),** newContentPane);
documentPane.Children.Add(newContentPane);
}
}
Then I added the following to the ViewModel to gain the effect I was going after:
public void OnNavigatedTo(NavigationContext navigationContext)
{
var viewParentInstance = DockinWindowChildObjectDictionary.GetInstance("Belt Plan");
viewParentInstance.IsSelected = true;
}
One hurdle done and on to the next. For a base to all the information in this post the ViewSwitchingNavigation.sln included with the PRISM 5.0 download will get you started. If you are wondering about the ConfigureDefaultRegionBehaviors() referenced in the adapter registration I got that from the StockTraderRI_Desktop.sln in the sample downloads.
I hope this post helps someone else that finds themselves in the same pickle this technology sandwich provides.
Sincerely
James

Why is passed object null? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I Pass an object OptionSelector to a different ViewModel. When passing it is not null. But on the receiving end it shows as null. Any ideas
My View is wrapped in this class.
public class CoreView : XboxApplicationPage, IRefAppNavigationItem
{
/// <summary>
/// Gets or sets navigation data for nested views.
/// </summary>
public object Data
{
get { return GetValue(DataProperty) as object; }
set { SetValue(DataProperty, value); }
}
/// <summary>
/// Identifies the Data dependency property.
/// </summary>
public static readonly DependencyProperty DataProperty =
DependencyProperty.Register("Data",typeof(object),typeof(CoreView),new PropertyMetadata(null));
/// <summary>
/// Gets or sets the viewModel used in this page.
/// </summary>
public CoreViewModel ViewModel
{
get
{
return (CoreViewModel)GetValue(ViewModelProperty);
}
set { SetValue(ViewModelProperty, value); }
}
/// <summary>
/// Sets the View.DataContext to the View.ViewModel.
/// </summary>
private void SetViewModel()
{
if (ViewModel != null)
{
try
{
if (this.Data != null)
{
ViewModel.Data = this.Data;
}
else
{
ViewModel.Data = this.Tag;
}
SetDataContext();
}
catch(Exception e)
{
Logger.Log("SetViewModel() error :" + e.StackTrace);
}
}
}
/// <summary>
/// Sets the DataContext to the ViewModel.
/// Override when additional actions might be required after setting the DataContext.
/// </summary>
protected virtual void SetDataContext()
{
this.DataContext = ViewModel;
}
/// <summary>
/// Handles on NavigateTo events.
/// </summary>
/// <param name="e">Event args.</param>
/// <remarks>This method is used to get the post navigation data and integrate into CoreView.</remarks>
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
if (DesignerProperties.IsInDesignTool)
{
return;
}
try
{
if (this.currentPageCulture != AmebaTV_XBOXApplication.Common.Resources.Localization.CurrentCulture)
{
UpdateLocalizedStrings(this);
}
Microsoft.Xbox.Controls.Localization.CultureChanged += Localization_CultureChanged;
var postNavigationState = IoC.Get<INavigationService>().GetPostNavigationData(NavigationContext);
if (postNavigationState != null)
{
this.Data = postNavigationState;
}
this.ViewModel.OnNavigatedTo();
if (legendService != null)
{
legendService.IsNavigateBackEnabled = true;
}
base.OnNavigatedTo(e);
}
catch (Exception ex)
{
Logger.Log("OnNavigatedTo : "+ex.Message);
}
}
}
/// <summary>
/// Base class for all ViewModels.
/// </summary>
public class CoreViewModel : ViewModelBase
{
/// <summary>
/// Field for Data.
/// </summary>
private object data;
/// <summary>
/// To be used with navigation to populate view models with initial content.
/// </summary>
public virtual void OnDataSet()
{
}
/// <summary>
/// Gets or sets ViewModel data.
/// </summary>
public object Data
{
get { return this.data; }
set
{
this.data = value;
RaisePropertyChanged("Data");
OnDataSet();
}
}
}
OptionSelectorData object
/// <summary>
/// Contains data for the options selector view.
/// </summary>
public class OptionSelectorData
{
/// <summary>
/// Gets or sets the list of options.
/// </summary>
public IList<string> Options { get; set; }
/// <summary>
/// Gets or sets the option title.
/// </summary>
public string Title { get; set; }
/// <summary>
/// Gets or sets the callback that will be invoked when an option is selected
/// </summary>
public Action<string> NotificationCallback { get; set; }
}
}
Command to trigger navigation
public class MoreOverflowViewModel : CoreViewModel
{
/// <summary>
/// Navigate to Property filter page
/// </summary>
public ICommand gotoViewPagebyCriteria
{
get
{
return new RelayCommand(() =>
{
OptionSelectorData option = new OptionSelectorData
{
Options = filterOptions, Title =
Localization.GetByLocalizationKey("OptionTitleFilter"),
NotificationCallback = OnFilterOptionsCallback
};
Messenger.Default.Send(new NavigateToMessage(new
Uri(PageListings.ViewPageByCriteria, UriKind.Relative), option));
});
}
}
}
Viewmodel to receive data, OnDataSet checks object and sets properties
public class ViewByCriteriaViewModel : CoreViewModel
{
/// <summary>
/// ondataset
/// </summary>
public override void OnDataSet()
{
option = this.Data as OptionSelectorData;
if (option != null)
{
OptionTitle = option.Title;
itemsSource = option.Options;
}
else
{
Logger.Log("NULL Option Data");
}
}
}
Try this. I'm thinking that this.Data isn't getting the object you think it is. If it's some other type, then as will return null.
public override void OnDataSet()
{
Logger.Log("this.Data = " + (this.Data == null ? "null" : this.Data.GetType().Name));
option = this.Data as OptionSelectorData;
if (option != null)
{
OptionTitle = option.Title;
itemsSource = option.Options;
}
else
{
Logger.Log("NULL Option Data");
}
}
Its seems I had a default OnNavigatedTo() that was overriding the method in the base class. Issue resolved.

Is there any way to know when the variables in IsolatedStorageSettings.ApplicationSettings are changed ?

I am trying to create a settings page for my windows phone 7 application and i am using this article as the basis http://msdn.microsoft.com/en-us/library/ff769510(v=vs.92).aspx so my application class is :
public class AppSettings
{
// Our isolated storage settings
IsolatedStorageSettings isolatedStore;
// The isolated storage key names of our settings
const string CheckBoxSettingKeyName = "CheckBoxSetting";
const string ListBoxSettingKeyName = "ListBoxSetting";
const string RadioButton1SettingKeyName = "RadioButton1Setting";
const string RadioButton2SettingKeyName = "RadioButton2Setting";
const string RadioButton3SettingKeyName = "RadioButton3Setting";
const string UsernameSettingKeyName = "UsernameSetting";
const string PasswordSettingKeyName = "PasswordSetting";
// The default value of our settings
const bool CheckBoxSettingDefault = true;
const int ListBoxSettingDefault = 0;
const bool RadioButton1SettingDefault = true;
const bool RadioButton2SettingDefault = false;
const bool RadioButton3SettingDefault = false;
const string UsernameSettingDefault = "";
const string PasswordSettingDefault = "";
/// <summary>
/// Constructor that gets the application settings.
/// </summary>
public AppSettings()
{
try
{
// Get the settings for this application.
isolatedStore = IsolatedStorageSettings.ApplicationSettings;
}
catch (Exception e)
{
Debug.WriteLine("Exception while using IsolatedStorageSettings: " + e.ToString());
}
}
/// <summary>
/// Update a setting value for our application. If the setting does not
/// exist, then add the setting.
/// </summary>
/// <param name="Key"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool AddOrUpdateValue(string Key, Object value)
{
bool valueChanged = false;
// If the key exists
if (isolatedStore.Contains(Key))
{
// If the value has changed
if (isolatedStore[Key] != value)
{
// Store the new value
isolatedStore[Key] = value;
valueChanged = true;
}
}
// Otherwise create the key.
else
{
isolatedStore.Add(Key, value);
valueChanged = true;
}
return valueChanged;
}
/// <summary>
/// Get the current value of the setting, or if it is not found, set the
/// setting to the default setting.
/// </summary>
/// <typeparam name="valueType"></typeparam>
/// <param name="Key"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public valueType GetValueOrDefault<valueType>(string Key, valueType defaultValue)
{
valueType value;
// If the key exists, retrieve the value.
if (isolatedStore.Contains(Key))
{
value = (valueType)isolatedStore[Key];
}
// Otherwise, use the default value.
else
{
value = defaultValue;
}
return value;
}
/// <summary>
/// Save the settings.
/// </summary>
public void Save()
{
isolatedStore.Save();
}
/// <summary>
/// Property to get and set a CheckBox Setting Key.
/// </summary>
public bool CheckBoxSetting
{
get
{
return GetValueOrDefault<bool>(CheckBoxSettingKeyName, CheckBoxSettingDefault);
}
set
{
AddOrUpdateValue(CheckBoxSettingKeyName, value);
Save();
}
}
/// <summary>
/// Property to get and set a ListBox Setting Key.
/// </summary>
public int ListBoxSetting
{
get
{
return GetValueOrDefault<int>(ListBoxSettingKeyName, ListBoxSettingDefault);
}
set
{
AddOrUpdateValue(ListBoxSettingKeyName, value);
Save();
}
}
/// <summary>
/// Property to get and set a RadioButton Setting Key.
/// </summary>
public bool RadioButton1Setting
{
get
{
return GetValueOrDefault<bool>(RadioButton1SettingKeyName, RadioButton1SettingDefault);
}
set
{
AddOrUpdateValue(RadioButton1SettingKeyName, value);
Save();
}
}
/// <summary>
/// Property to get and set a RadioButton Setting Key.
/// </summary>
public bool RadioButton2Setting
{
get
{
return GetValueOrDefault<bool>(RadioButton2SettingKeyName, RadioButton2SettingDefault);
}
set
{
AddOrUpdateValue(RadioButton2SettingKeyName, value);
Save();
}
}
/// <summary>
/// Property to get and set a RadioButton Setting Key.
/// </summary>
public bool RadioButton3Setting
{
get
{
return GetValueOrDefault<bool>(RadioButton3SettingKeyName, RadioButton3SettingDefault);
}
set
{
AddOrUpdateValue(RadioButton3SettingKeyName, value);
Save();
}
}
/// <summary>
/// Property to get and set a Username Setting Key.
/// </summary>
public string UsernameSetting
{
get
{
return GetValueOrDefault<string>(UsernameSettingKeyName, UsernameSettingDefault);
}
set
{
AddOrUpdateValue(UsernameSettingKeyName, value);
Save();
}
}
/// <summary>
/// Property to get and set a Password Setting Key.
/// </summary>
public string PasswordSetting
{
get
{
return GetValueOrDefault<string>(PasswordSettingKeyName, PasswordSettingDefault);
}
set
{
AddOrUpdateValue(PasswordSettingKeyName, value);
Save();
}
}
}
Everything is working fine and i am able to update the settings value from a xaml settings page and i can access the setting values in my main application using
IsolatedStorageSettings Settings;
Settings = IsolatedStorageSettings.ApplicationSettings;
string checkboxvalue = (string)Settings["CheckBoxSetting"];
Now what i want is to be able to know when a value in the application settings is changed/updated , so that i can perform a action when settings are updated .
Similar to the other answer but more concrate. If you already use properties to work with, implement INotifyPropertyChanged interface, write your own RaisePropertyChanged/NotifyPropertyChanged and in the destination class subscribe to PropertyChanged event.
IsolatedStorageSettings.ApplicationSettings is a Dictionary. It is not observable by default.
There are lots of ways you could do this but the easiest way to get startred is if you just query the settings when you need to know them.
The other simple alternative woudl be for your AppSettings object to raise events when something was changed and have the relevant other classes subscribe to them.

Categories