i can't convert this codes to vb.net. please help me. thank you.
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace RssReader.Common
{
/// <summary>
/// Provides a standard change-notification implementation.
/// </summary>
public abstract class BindableBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName] string propertyName = null) =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
protected bool SetProperty<T>(ref T storage, T value,
[CallerMemberName] String propertyName = null)
{
if (object.Equals(storage, value)) return false;
storage = value;
OnPropertyChanged(propertyName);
return true;
}
}
}
i cant convert this code's to vb.net. thank you very much
Not having the context of what you need this class for (its only a snippet after all, I attempted to perform a compilable conversion for you. What this means is that the compiler says the syntax is right. Based on my extensive knowledge of VB.NET and my growing C# skills I think this is likely the solution you need.
The real issue that most code converters miss here is that when you specify a default value for a parameter in VB.NET, you MUST use the keyword "Optional" in front of it. To their detriment most converters will also put the Attribute definition before the word Byval and after the word Optional which is not correct. To correctly make a variable optional, you must put that keyword directly before the Byval. Thus the only place for the attribute definition to go is before the word Optional.
Caveat.
Now, the compiler likes what I wrote, but I have no idea if it will work in your context having no access to the wider codebase that your calling it from.
Hope this helps, Code is below.
Imports System
Imports System.ComponentModel
Imports System.Runtime.CompilerServices
Namespace RssReader.Common
''' <summary>
''' Provides a standard change-notification implementation.
''' </summary>
Public MustInherit Class BindableBase
Implements INotifyPropertyChanged
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Public Sub OnPropertyChanged(<CallerMemberName> Optional ByVal propertyName As String = Nothing)
PropertyChangedEvent?.Invoke(Me, New PropertyChangedEventArgs(propertyName))
End Sub
Protected Function SetProperty(Of T)(ByRef storage As T, ByVal value As T, <CallerMemberName> Optional ByVal propertyName As String = Nothing) As Boolean
If Object.Equals(storage, value) Then
Return False
End If
storage = value
OnPropertyChanged(propertyName)
Return True
End Function
End Class
End Namespace
Related
im trying to learn how to use WPF data binding.
I have a control, and i want to change the value f a property in the control.
<somecontrol Value="{Binding GoodRange}">
I created the property in the MainWindow Class as follows:
Public Property GoodRange As Double
Get
Return m_GoodRange
End Get
Set(value As Double)
m_GoodRange = value
End Set
End Property
Private m_GoodRange As Double
Inside the Mainwindow class i added the following to the sub New()
Public Sub New()
InitializeComponent()
GoodRange = 3000
Me.DataContext = Me
End Sub
So far so good, hwen i launch the program the value 3000 is passed to the control.
Now, during runtime i want to change the property for example when a user clicks on a button, or on a timed event eg:
Private Sub UpdateValue()
GoodRange = 2800
End Sub
When i do this, the value on the control is not updated. im trying to understand how i can trigger the control to update.
I have googled for 4 hours try try and understand, and i have found and tried a lot of answers on google, but usually these answers are for custom controls or custom classes or using the .datacontex method which i cant use as multiple property's will need to be changed.
I would be greatfull for any help you guys can offer.
Thank you/
As Clemens says in the comment to your question, you really need to do some research on MVVM, which has Data Binding as its heart and soul. An excellent article to start is of course the classic from Josh Smith, MVVM Design Pattern
In the meantime, as a minimum functional example, you should create a class as shown below that implements the INotifyPropertyChanged interface (code below is in C#):
public class myViewModel : INotifyPropertyChanged
{
private double goodRange = 3000;
public double GoodRange
{
get
{
return goodRange;
}
set
{
if (value != goodRange)
{
goodRange = value;
NotifyPropertyChanged("GoodRange");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propertyName)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
In the code behind of your window:
Dim mVM as myViewModel
Public Sub New()
InitializeComponent()
mVM = new myViewModel()
Me.DataContext = mVM
End Sub
Private Sub UpdateValue()
mVM.GoodRange = 2800
End Sub
I found this tutorial and I was able to implement it.
How can I disable a button when an errors occurs?
I searched a lot over the net, but I can't find a piece of code that resembles mine. (Yes, I know there about a zillion threads about this matter, but I just don't understand it.)
Here is my current code, it's a bit lengthy:
Public Class GradeVm
Implements IDataErrorInfo
Public Interface IDataErrorInfo
Default ReadOnly Property Item(columnName As String) As String
ReadOnly Property [Error]() As String
End Interface
#Region "Properties"
Property Grade As Integer
Property Adjust As Integer
#End Region
Public ReadOnly Property [Error] As String Implements IDataErrorInfo.Error
Get
Return "Error"
End Get
End Property
Default Public ReadOnly Property Item(columnName As String) As String Implements IDataErrorInfo.Item
Get
Select Case columnName
Case "Grade"
If IsNumeric(Me.Grade) = False Then
Return [Error]
End If
Case "Adjust"
If IsNumeric(Me.Adjust) = False Then
Return [Error]
End If
End Select
Return ""
End Get
End Property
End Class
This combined with the code here did the trick! Finally!! :)
If you're binding button to RelayCommand, you can disable the button by setting CanExecute() function to return False :
Public Function CanExecute(parameter As Object) As Boolean Implements ICommand.CanExecute
Return False
End Function
But RelayCommand doesn't meant to be used this way. I'd suggest to pick a MVVM framework (MVVMLight for example), and you'll get better RelayCommand implementation ready to use. Then you can use it like this :
Private _myCommand As RelayCommand = New RelayCommand(Me.ExecuteCommand, Me.CanExecuteCommand)
Public ReadOnly Property MyCommand As ICommand
Get
Return Me._myCommand
End Get
End Property
Private Sub ExecuteCommand()
......
End Sub
Private Function CanExecuteCommand() As Boolean
'do logic to disable (return false) or enable (return true) button
'based on specific criteria
'just for example, disable the button forever :
Return False
End Function
UPDATE :
Why you deleted your RelayCommand implementation? The 1st approach meant, use RelayCommand in your original post, but change return value of CanExecute() function to False instead of True. Then you can use it in ViewModel like so :
Private _myCommand As RelayCommand = New RelayCommand()
Public ReadOnly Property MyCommand As ICommand
Get
Return Me._myCommand
End Get
End Property
I am using VB.NET and WPF within Visual Studio 2010 Express.
Currently, I have:
A DataGrid by the name of downloadListDG. This has a column which is a template containing an image.
An ObservableCollection of a custom DownloadListItem class.
This DownloadListItem has a public property which is another custom class.
This class has a private dim which is a StateType (a custom enum), and a public readonly property which returns a string depending on what the StateType is (actually an image URI if you're curious).
The DownloadListItem also has a public property which just returns the StateType (this is just for binding purposes)
My problem is that whenever the StateType changes, the image column in the DataGrid does not change. I have been trying to use the IPropertyChangedNofity, but nothing changes, so either I'm using it incorrectly or I need to use another method.
Implements INotifyPropertyChanged
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
AddHandler ControllerRef.StateChanged, AddressOf StateChangeHandler
Private Sub StateChangeHandler(NewState As State)
MsgBox(NewState)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("CurrentState"))
End Sub
Thanks in advance
Make sure the PropertyChanged event is notifying the UI of the property name you are bound to, not the property that triggers the change. Example:
Imports System.ComponentModel
Public Class DownloadListItem : Implements INotifyPropertyChanged
Friend Enum StateEnum
State1 = 0
State2 = 1
End Enum
Private _CurrentState As StateEnum
Private Sub ChangeEnumValue(NewValue As StateEnum)
_CurrentState = NewValue
OnPropertyChanged("ImageURI")
End Sub
Public ReadOnly Property ImageURI As String
Get
' TODO: Implement conditional logic to return proper value based on CurrentState Enum
End Get
End Property
Public Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged
Protected Sub OnPropertyChanged(PropertyName As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(PropertyName))
End Sub
End Class
I have a datagrid bound to an observable collection of objects. What I want to do is have a button that will execute a method of the object representing the row of the button that was clicked. So what I have now is something like this:
<DataGridTemplateColumn Header="Command">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Name="cmdCommand" Click="{Binding Command}"
Content="Command"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
Which doesn't work and reports the following error:
Click="{Binding Command}" is not valid. '{Binding Command}' is not a valid event handler method name. Only instance methods on the generated or code-behind class are valid.
I've looked at command binding but that looks like it would just end up going to a single external command instead of to the object bound to the row. I have it working using an event handler on the code behind and then routing it to the item bound to the selected row (since the row gets selected when the button is clicked) but that seems like poor way of handing this and I assume I'm just missing something here.
I do this all the time. Here's a look at an example and how you would implement it.
Change your XAML to use the Command property of the button instead of the Click event. I am using the name SaveCommand since it is easier to follow then something named Command.
<Button Command="{Binding Path=SaveCommand}" />
Your CustomClass that the Button is bound to now needs to have a property called SaveCommand of type ICommand. It needs to point to the method on the CustomClass that you want to run when the command is executed.
public MyCustomClass
{
private ICommand _saveCommand;
public ICommand SaveCommand
{
get
{
if (_saveCommand == null)
{
_saveCommand = new RelayCommand(
param => this.SaveObject(),
param => this.CanSave()
);
}
return _saveCommand;
}
}
private bool CanSave()
{
// Verify command can be executed here
}
private void SaveObject()
{
// Save command execution logic
}
}
The above code uses a RelayCommand which accepts two parameters: the method to execute, and a true/false value of if the command can execute or not. The RelayCommand class is a separate .cs file with the code shown below. I got it from Josh Smith :)
/// <summary>
/// A command whose sole purpose is to
/// relay its functionality to other
/// objects by invoking delegates. The
/// default return value for the CanExecute
/// method is 'true'.
/// </summary>
public class RelayCommand : ICommand
{
#region Fields
readonly Action<object> _execute;
readonly Predicate<object> _canExecute;
#endregion // Fields
#region Constructors
/// <summary>
/// Creates a new command that can always execute.
/// </summary>
/// <param name="execute">The execution logic.</param>
public RelayCommand(Action<object> execute)
: this(execute, null)
{
}
/// <summary>
/// Creates a new command.
/// </summary>
/// <param name="execute">The execution logic.</param>
/// <param name="canExecute">The execution status logic.</param>
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
}
#endregion // Constructors
#region ICommand Members
[DebuggerStepThrough]
public bool CanExecute(object parameters)
{
return _canExecute == null ? true : _canExecute(parameters);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameters)
{
_execute(parameters);
}
#endregion // ICommand Members
}
You have various possibilies. The most simple and the most ugly is:
XAML
<Button Name="cmdCommand" Click="Button_Clicked" Content="Command"/>
Code Behind
private void Button_Clicked(object sender, RoutedEventArgs e) {
FrameworkElement fe=sender as FrameworkElement;
((YourClass)fe.DataContext).DoYourCommand();
}
Another solution (better) is to provide a ICommand-property on your YourClass. This command will have already a reference to your YourClass-object and therefore can execute an action on this class.
XAML
<Button Name="cmdCommand" Command="{Binding YourICommandReturningProperty}" Content="Command"/>
Because during writing this answer, a lot of other answers were posted, I stop writing more. If you are interested in one of the ways I showed or if you think I have made a mistake, make a comment.
Here is the VB.Net rendition of Rachel's answer above.
Obviously the XAML binding is the same...
<Button Command="{Binding Path=SaveCommand}" />
Your Custom Class would look like this...
''' <summary>
''' Retrieves an new or existing RelayCommand.
''' </summary>
''' <returns>[RelayCommand]</returns>
Public ReadOnly Property SaveCommand() As ICommand
Get
If _saveCommand Is Nothing Then
_saveCommand = New RelayCommand(Function(param) SaveObject(), Function(param) CanSave())
End If
Return _saveCommand
End Get
End Property
Private _saveCommand As ICommand
''' <summary>
''' Returns Boolean flag indicating if command can be executed.
''' </summary>
''' <returns>[Boolean]</returns>
Private Function CanSave() As Boolean
' Verify command can be executed here.
Return True
End Function
''' <summary>
''' Code to be run when the command is executed.
''' </summary>
''' <remarks>Converted to a Function in VB.net to avoid the "Expression does not produce a value" error.</remarks>
''' <returns>[Nothing]</returns>
Private Function SaveObject()
' Save command execution logic.
Return Nothing
End Function
And finally the RelayCommand class is as follows...
Public Class RelayCommand : Implements ICommand
ReadOnly _execute As Action(Of Object)
ReadOnly _canExecute As Predicate(Of Object)
Private Event ICommand_CanExecuteChanged As EventHandler Implements ICommand.CanExecuteChanged
''' <summary>
''' Creates a new command that can always execute.
''' </summary>
''' <param name="execute">The execution logic.</param>
Public Sub New(execute As Action(Of Object))
Me.New(execute, Nothing)
End Sub
''' <summary>
''' Creates a new command.
''' </summary>
''' <param name="execute">The execution logic.</param>
''' <param name="canExecute">The execution status logic.</param>
Public Sub New(execute As Action(Of Object), canExecute As Predicate(Of Object))
If execute Is Nothing Then
Throw New ArgumentNullException("execute")
End If
_execute = execute
_canExecute = canExecute
End Sub
<DebuggerStepThrough>
Public Function CanExecute(parameters As Object) As Boolean Implements ICommand.CanExecute
Return If(_canExecute Is Nothing, True, _canExecute(parameters))
End Function
Public Custom Event CanExecuteChanged As EventHandler
AddHandler(ByVal value As EventHandler)
AddHandler CommandManager.RequerySuggested, value
End AddHandler
RemoveHandler(ByVal value As EventHandler)
RemoveHandler CommandManager.RequerySuggested, value
End RemoveHandler
RaiseEvent(ByVal sender As Object, ByVal e As EventArgs)
If (_canExecute IsNot Nothing) Then
_canExecute.Invoke(sender)
End If
End RaiseEvent
End Event
Public Sub Execute(parameters As Object) Implements ICommand.Execute
_execute(parameters)
End Sub
End Class
Hope that helps any VB.Net developers!
Click is an event. In your code behind, you need to have a corresponding event handler to whatever you have in the XAML. In this case, you would need to have the following:
private void Command(object sender, RoutedEventArgs e)
{
}
Commands are different. If you need to wire up a command, you'd use the Commmand property of the button and you would either use some pre-built Commands or wire up your own via the CommandManager class (I think).
On Xamarin Forms, the ugliest and most straightforward version:
Xaml:
<Button Margin="0,10,0,0"
Text="Access galery"
Clicked="OpenGalery"
BackgroundColor="{StaticResource Primary}"
TextColor="White" />
then: in .cs
private async void OpenGalery(object sender, EventArgs e)
{
//do your bidding
}
Someone please help me understand why this binding does not work...
I have a class called SelectionManager with a property called 'dates' which is populated by a WCF service. The property is an array of structs which bundles a DateTime and an integer count of business objects.
public class SelectionManager : INotifyPropertyChanged {
... other properties ...
public DQMServiceDateCountPair[] dates { get; private set; }
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propertyName) {
if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); }
}
I have another class called DateSelector which has a DependencyProperty called 'pairs' setup to be the binding target of 'dates'.
public partial class DateSelector : UserControl {
... other stuff ...
public static readonly DependencyProperty pairsProperty = DependencyProperty.Register(
"pairs",
typeof(DQMServiceDateCountPair[]),
typeof(DateSelector),
new PropertyMetadata(new DQMServiceDateCountPair[0])
);
public DQMServiceDateCountPair[] pairs {
get { return (DQMServiceDateCountPair[])GetValue(pairsProperty); }
set {
Debug.WriteLine("adding dates");
SetValue(pairsProperty, value);
dateMode = DateMode.Years;
}
}
}
In my MainPage.xaml, I have a line like this:
<date:DateSelector x:Name="dateSelector" pairs="{Binding dates}" />
It's weird, because all my other bindings in MainPage.xaml update correctly, including a ComboBox bound to 'dates'. My UserControl however, will not update. The Debug.Writeline doesn't get called in the set statement of the 'pairs' property.
In playing around with it, I've tried making the DQMServiceDateCountPair[] property into an ObservableCollection and implementing INotifyCollectionChanged, but that doesn't help.
If I leave either the source property or the target property as an array, and make the other an ObservableCollection, then I get a binding error that says it can't automatically convert one to the other, so Silverlight seems aware of the binding, it just doesn't update it.
Can anyone help?
P.S. I'm using Silverlight 3.
Try changing your code as follows:
1. Add DataMember/DataContract attributes
2. Make "set" public
[DataContract]
public class SelectionManager : INotifyPropertyChanged {
[DataMember]
public DQMServiceDateCountPair[] dates { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propertyName) {
if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); }
}
Whats actually wierd is that the other stuff is working when you've coded your class the way you have. My guess is that the dates array gets set by some code that runs internally in your selection manager on completion of a WCF request.
Howerver whilst you have implemented INotifyPropertyChanged you aren't actually raising the event that it defines. You can't really combine INotifyPropertyChanged with the Auto-property C# syntax. You need this:-
public SelectionManager : INotifyPropertyChanged
{
private DQMServiceDateCountPair[] myDates;
public DQMServiceDateCountPair[] dates
{
get { return myDates; }
set
{
myDates = value;
NotifyPropertyChanged("dates");
}
// rest of your code
}
So, here's what what going on. The binding has been working perfectly well this whole time. For the past week I've been struggling with this, it's been happily updating along--but because of a faulty assumption on my part, I could never see it.
In case anyone else harbors this faulty assumption, let me spell it out:
The GetValue and SetValue calls are not made automatically by virtue of the fact that you are declaring a Dependency Property. The "new PropertyMetadata()" part of the declaration has an overload that takes a callback method. In this callback method, you have to set the property value yourself. For instance, in my code, I made this the PropertyMetadata call:
new PropertyMetadata(new PropertyChangedCallback(OnPairsPropertyChanged))
and the callback method reads like this:
private static void OnPairsPropertyChanged( DependencyObject d, DependencyPropertyChangedEventArgs e ) {
((DateSelector)d).pairs = (DQMServiceDateCountPair[])e.NewValue;
}
Thanks to everyone who tried to help!