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.
Related
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?
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;
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.
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.
I currently have a requirement to notify my application user if any fields have been changed/updated on a View.
For example, if the user changes a date field on the View and then tries to close the View, the application would display a message asking the user to Continue and lose changes or Cancel so that they can click the Save button.
Problem is: How do I detect that any of the data fields changed in the View?
Hope this makes sense, than you in advance, regards,
One approach you can take is to leverage the IChangeTracking and INotifyPropertyChanged interfaces.
If you create an abstract base class that your view models inherit from (ViewModelBase) which implements the IChangeTracking and INotifyPropertyChanged interfaces, you can have your view model base attach to notification of property changes (in effect signaling that the view model has been modified) and which will set the IsChanged property to true to indicate that the view model is 'dirty'.
Using this approach, you are relying on property change notification via data binding to track changes and would reset the change tracking after any commits are made.
In the case you described you could handle the Unloaded or Closing event of your view to inspect the DataContext; and if the DataContext implements IChangeTracking you can use the IsChanged property to determine if any unaccepted changes have been made.
Simple example:
/// <summary>
/// Provides a base class for objects that support property change notification
/// and querying for changes and resetting of the changed status.
/// </summary>
public abstract class ViewModelBase : IChangeTracking, INotifyPropertyChanged
{
//========================================================
// Constructors
//========================================================
#region ViewModelBase()
/// <summary>
/// Initializes a new instance of the <see cref="ViewModelBase"/> class.
/// </summary>
protected ViewModelBase()
{
this.PropertyChanged += new PropertyChangedEventHandler(OnNotifiedOfPropertyChanged);
}
#endregion
//========================================================
// Private Methods
//========================================================
#region OnNotifiedOfPropertyChanged(object sender, PropertyChangedEventArgs e)
/// <summary>
/// Handles the <see cref="INotifyPropertyChanged.PropertyChanged"/> event for this object.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">A <see cref="PropertyChangedEventArgs"/> that contains the event data.</param>
private void OnNotifiedOfPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e != null && !String.Equals(e.PropertyName, "IsChanged", StringComparison.Ordinal))
{
this.IsChanged = true;
}
}
#endregion
//========================================================
// IChangeTracking Implementation
//========================================================
#region IsChanged
/// <summary>
/// Gets the object's changed status.
/// </summary>
/// <value>
/// <see langword="true"/> if the object’s content has changed since the last call to <see cref="AcceptChanges()"/>; otherwise, <see langword="false"/>.
/// The initial value is <see langword="false"/>.
/// </value>
public bool IsChanged
{
get
{
lock (_notifyingObjectIsChangedSyncRoot)
{
return _notifyingObjectIsChanged;
}
}
protected set
{
lock (_notifyingObjectIsChangedSyncRoot)
{
if (!Boolean.Equals(_notifyingObjectIsChanged, value))
{
_notifyingObjectIsChanged = value;
this.OnPropertyChanged("IsChanged");
}
}
}
}
private bool _notifyingObjectIsChanged;
private readonly object _notifyingObjectIsChangedSyncRoot = new Object();
#endregion
#region AcceptChanges()
/// <summary>
/// Resets the object’s state to unchanged by accepting the modifications.
/// </summary>
public void AcceptChanges()
{
this.IsChanged = false;
}
#endregion
//========================================================
// INotifyPropertyChanged Implementation
//========================================================
#region PropertyChanged
/// <summary>
/// Occurs when a property value changes.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
#endregion
#region OnPropertyChanged(PropertyChangedEventArgs e)
/// <summary>
/// Raises the <see cref="INotifyPropertyChanged.PropertyChanged"/> event.
/// </summary>
/// <param name="e">A <see cref="PropertyChangedEventArgs"/> that provides data for the event.</param>
protected void OnPropertyChanged(PropertyChangedEventArgs e)
{
var handler = this.PropertyChanged;
if (handler != null)
{
handler(this, e);
}
}
#endregion
#region OnPropertyChanged(string propertyName)
/// <summary>
/// Raises the <see cref="INotifyPropertyChanged.PropertyChanged"/> event for the specified <paramref name="propertyName"/>.
/// </summary>
/// <param name="propertyName">The <see cref="MemberInfo.Name"/> of the property whose value has changed.</param>
protected void OnPropertyChanged(string propertyName)
{
this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}
#endregion
#region OnPropertyChanged(params string[] propertyNames)
/// <summary>
/// Raises the <see cref="INotifyPropertyChanged.PropertyChanged"/> event for the specified <paramref name="propertyNames"/>.
/// </summary>
/// <param name="propertyNames">An <see cref="Array"/> of <see cref="String"/> objects that contains the names of the properties whose values have changed.</param>
/// <exception cref="ArgumentNullException">The <paramref name="propertyNames"/> is a <see langword="null"/> reference (Nothing in Visual Basic).</exception>
protected void OnPropertyChanged(params string[] propertyNames)
{
if (propertyNames == null)
{
throw new ArgumentNullException("propertyNames");
}
foreach (var propertyName in propertyNames)
{
this.OnPropertyChanged(propertyName);
}
}
#endregion
}
In MVVM a View is binded to a View-Model which in turn is binded to a Model.
The view can not be dirty, since it's changes are reflected immediately to the View-Model.
If you want changes to be applied to Model only on "OK" or "Accept",
bind View to a View-Model that doesn't apply changes to Model,
until an ApplyCommand or AcceptCommand (that you define and implement) is executed.
(The commands that the View is binded to are implemented by the View-Model.)
Example - VM:
public class MyVM : INotifyPropertyChanged
{
public string MyText
{
get
{
return _MyText;
}
set
{
if (value == _MyText)
return;
_MyText = value;
NotifyPropertyChanged("MyText");
}
}
private string _MyText;
public string MyTextTemp
{
get
{
return _MyTextTemp;
}
set
{
if (value == _MyTextTemp)
return;
_MyTextTemp = value;
NotifyPropertyChanged("MyTextTemp");
NotifyPropertyChanged("IsTextDirty");
}
}
private string _MyTextTemp;
public bool IsTextDirty
{
get
{
return MyText != MyTextTemp;
}
}
public bool IsMyTextBeingEdited
{
get
{
return _IsMyTextBeingEdited;
}
set
{
if (value == _IsMyTextBeingEdited)
return;
_IsMyTextBeingEdited = value;
if (!value)
{
MyText = MyTextTemp;
}
NotifyPropertyChanged("IsMyTextBeingEdited");
}
}
private bool _IsMyTextBeingEdited;
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
Example - View:
<Label Content="{Binding MyText}" />
<!-- You can translate the events to commands by using a suitable framework -->
<!-- or use code behind to update a new dependency property as in this example -->
<TextBox
LostFocus="TextBox_LostFocus"
GotFocus="TextBox_GotFocus"
Text="{Binding Path=MyTextTemp, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
/>
Example - view - code behind:
public MainWindow()
{
InitializeComponent();
SetBinding(IsTextBoxFocusedProperty,
new Binding
{
Path = new PropertyPath("IsMyTextBeingEdited"),
Mode = BindingMode.OneWayToSource,
});
}
private void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
IsTextBoxFocused = false;
}
private void TextBox_GotFocus(object sender, RoutedEventArgs e)
{
IsTextBoxFocused = true;
}
#region IsTextBoxFocused
/// <summary>
/// Gets or Sets IsTextBoxFocused
/// </summary>
public bool IsTextBoxFocused
{
get
{
return (bool)this.GetValue(IsTextBoxFocusedProperty);
}
set
{
this.SetValue(IsTextBoxFocusedProperty, value);
}
}
/// <summary>
/// The backing DependencyProperty behind IsTextBoxFocused
/// </summary>
public static readonly DependencyProperty IsTextBoxFocusedProperty = DependencyProperty.Register(
"IsTextBoxFocused", typeof(bool), typeof(MainWindow), new PropertyMetadata(default(bool)));
#endregion
Idea: check entitystate:
Problem is that this refers to the whole VIEW, so when a new participant (refreshes form) is selected before any editing, the value is also "Modified". After a save, if nothing else changes and we don’t switch participants, the value is "Unchanged"