Saturday, February 14, 2015

How to dynamically show/hide a tool item based on the availability of an assembly


Dynamically show/hide a tool item based on the availability of an assembly with LinsUIWPF Suite.
If a command is located inside an assembly which is optional, how to show/hide a tool item, which is associated to the command, based on the availability of the assembly. This page will show you the way to do it.

Prerequisite: Follow the link How to create a new project to create your application with LinsUIWPF Suite.

1.     Assume you have an assembly AdvanceOpertaion.dll,
First step, create a command inside the assembly AdvanceOpertaion.dll as follows,
namespace AdvanceOperation.Commands
{
class AdvanceOperationCommand : LinsUICommand
{
public AdvanceOperationCommand(long nCommandID, object data) : base(nCommandID, data)
{
}

public override void Execute(object parameter)
{
base.Execute(parameter);
///////////////////////////////////
// Execute your command here
///////////////////////////////////
}

public override bool CanExecute(object parameter)
{
return true;
}
}
}

Second step, create the following initialization class and method inside the assembly AdvanceOpertaion.dll as follows,
namespace AdvanceOperation
{
public static partial class InitializeAdvanceOperation
{
public static void Initialize()
{
CreateCommands();
}

private static void CreateCommands()
{
long nCommandID = 1002;
LinsUICommand command = new AdvanceOperationCommand(nCommandID, null);
}
}
}

Third step, inside your xaml file in your application, bind the tool item’s visibility as follows,
<src:SurfaceSlider
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:src="clr-namespace:LinsUIWPF;assembly=LinsUIWPF" 
x:Class="Demo.SurfaceSliders.MainSurfaceSlider" 
xmlns:localConverters="clr-namespace:Demo.Converters"
mc:Ignorable="d" Height="435" Width="1130">

<src:SurfaceSlider.Resources>
<ResourceDictionary>
<localConverters:AdvanceOperationVisibilityConverter x:Key="advanceOperationVisibilityConverter"/>
</ResourceDictionary>
</src:SurfaceSlider.Resources>

<!--------------------------------------------->   
<src:SurfaceSliderTasksPanel>
<src:TaskGroup>
<src:TaskButton ButtonType="LARGE" CommandID="1002" Visibility="{Binding Path=CommandID, RelativeSource={RelativeSource Self},Converter={StaticResource advanceOperationVisibilityConverter}}">
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<TextBlock Grid.Row="0" Text="Advance Operation" FontWeight="Thin" FontSize="12" FontStyle="Italic" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,10,0,0"/>
</Grid>
</src:TaskButton>
</src:TaskGroup>
</src:SurfaceSliderTasksPanel

</src:SurfaceSlider>

Fourth step, create a converter in your application as follows,
namespace Demo.Converters
{
public class AdvanceOperationVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
// Check to see whether value is present or not.
long commandID = (long)value;
LinsUICommand command = null;
return LinsUICommandManager.TryGetCommand(commandID, out command) ? Visibility.Visible : Visibility.Hidden;
}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}
}
}

Finally add the following codes at the place where you want to load and initialize the specified assembly, AdvanceOpertaion.dll,
string csAdvanceOperationAssemblyFullName = C:\\Users\\Projects\\Application\\Release\\AdvanceOpertaion.dll”;
LinsUIWPFUtil.InitializeAssembly(
csAdvanceOperationAssemblyFullName,
"AdvanceOpertaion.InitializeAdvanceOperation",
"Initialize",
null);

When you run your application, the taskbutton “Advance Operation” will be hidden until the assembly AdvanceOpertaion.dll is loaded.
   

No comments:

Post a Comment