Author Topic: How can I add a button to BundleToolbarRegion?  (Read 2310 times)

Offline Gabi

  • Jr. Member
  • **
  • Posts: 64
  • Karma: 0
How can I add a button to BundleToolbarRegion?
« on: October 19, 2018, 02:37:47 PM »
Hello.

I am trying to add a button to the BundleToolbarRegion of the Interaction view. I have read in the documentation that this region contains a BundleCustomButtonRegion which is a Multi-View, which means I should be able to add things to it (provided it is visible, which to me is a big 'if').

Here's what I have done so far.

Code for the button itself (I must admit that I do not know WPF. I have made a few views in the past through a mixture of copy-pasting and guesswork. So if there are any errors in my code, please let me know. I know this compiles, but that is not saying much.)

[code]<UserControl x:Class="[REDACTED].CustomViews.SurveyButton"
            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:common="clr-namespace:Genesyslab.Desktop.WPFCommon;assembly=Genesyslab.Desktop.WPFCommon"
xmlns:commonControls="clr-namespace:Genesyslab.Desktop.WPFCommon.Controls;assembly=Genesyslab.Desktop.WPFCommon"
            mc:Ignorable="d"
            d:DesignHeight="26" d:DesignWidth="26">
    <Grid>
        <commonControls:InteractionToolBarButton Click="buttonSurvey_Click" Name="buttonSurvey" ButtonStyle="{Binding ButtonStyle}"
                                          ToolTip="{Binding ToolTipButtonReply}"
                                          AutomationProperties.Name="{Binding PropertiesNameButtonReply}">
    <StackPanel Orientation="Horizontal">
            <commonControls:MagicImage Width="24" Height="24" Source="Survey.png"/>
        </StackPanel>
  </commonControls:InteractionToolBarButton>
  </Grid>
</UserControl>[/code]

Code for the interface:

[code]public interface ISurveyButtonView : IView
    {
        IInteractionViewModel Model { get; set; }
    }[/code]

Code for the xaml.cs:

[code]public partial class SurveyButton : UserControl, ISurveyButtonView
    {
        readonly IObjectContainer container;
        readonly IViewEventManager viewEventManager;

        public SurveyButton(IInteractionViewModel viewModel, IObjectContainer container, IViewEventManager viewEventManager)
        {
            this.container = container;
            this.viewEventManager = viewEventManager;
            this.Model = viewModel;//Se le puede pedir interaction.
            InitializeComponent();
        }

        private void buttonSurvey_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            //TODO
        }

        public Genesyslab.Desktop.Modules.Windows.Interactions.IInteractionViewModel Model
        {
            get { return this.DataContext as IInteractionViewModel; }
            set { this.DataContext = value; }
        }

        public object Context { get; set; }

        public void Create()
        {
            if (!(Model.Interaction is IInteractionInboundEmail)){
                this.Visibility = Visibility.Hidden;//I may need to change this but that's another story.
            }
        }

        public void Destroy()
        {
           
        }
    }[/code]

And finally, code for the addition of the view to the region within the initialization of the module:

[code]container.RegisterType<ISurveyButtonView, SurveyButton>();
            viewManager.ViewsByRegionName["BundleCustomButtonRegion"].Add(
                new ViewActivator() { ViewType = typeof(ISurveyButtonView), ViewName = "SurveyButton", ActivateView = true}
            );[/code]

I have tried debugging this with different types of interactions and the button view is never created (the Create method is not invoked and the button cannot be seen).

Could anyone please tell me what I'm missing and/or doing wrong?

Thank you in advance.


Offline Leon

  • Newbie
  • *
  • Posts: 8
  • Karma: 0
Re: How can I add a button to BundleToolbarRegion?
« Reply #1 on: October 22, 2018, 12:46:17 PM »
Why don't you try to create and use you own ViewModel instead of using IInteractionViewModel???

For example :

Interface of ViewModel :

[code]
    public interface ICustomButtonViewModel
    {
        bool IsVisible { get; set; }

        void OpenForm();
    }
[/code]

Interface of View: ICustomButtonView
[code]
    public interface ICustomButtonView : IView
    {
        ICustomButtonViewModel Model { get; set; }
    }
[/code]

Class of ViewModel :  CustomButtonViewModel

[code]
public class CustomButtonViewModel : ICustomButtonViewModel, INotifyPropertyChanged
    {
              ///Your implementation and code
    }
[/code]

Class of View : CustomButtonView (Code behind xaml)

[code]
public partial class CustomButtonView : UserControl, ICustomButtonView
    {
          ///Your implementation and code
    }
[/code]


Class of Module

[code]
public void Initialize(){
                      //Register and mapping your interface with the class into the container
                    _container.RegisterType<ICustomButtonView, CustomButtonView>();
                    _container.RegisterType<ICustomButtonViewModel, CustomButtonViewModel>();
                    _viewManager.ViewsByRegionName["BundleCustomButtonRegion"].Insert(0, new ViewActivator
                    {
                        ViewType = typeof(ICustomButtonView),
                        ActivateView = true,
                        ViewName = "CustomButtonView"
                    });
}
[/code]


I don't have had work with Email but you could try this to get the Interaction at the context Property in the create method :

[code]
public void Create()
        {
            try
            {
                IDictionary<string, object> context = Context as IDictionary<string, object>;

                if (context != null)
                {
                    if (context.ContainsKey("Interaction"))
                    {
                        IInteractionVoice interactionVoice = context["Interaction"] as IInteractionVoice;
                        if (interactionVoice != null)
                        {
                            _interactionVoice = interactionVoice;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.Error("Create::ERROR", ex);
            }
        }
[/code]

Also you can look at the samples who come in the installation for developer of WorkspaceDesktopEdition.

Offline Gabi

  • Jr. Member
  • **
  • Posts: 64
  • Karma: 0
Re: How can I add a button to BundleToolbarRegion?
« Reply #2 on: November 02, 2018, 12:07:26 PM »
Thank you for your response. The samples don't add anything to that region. Fortunately, I got help from someone who had more experience with views, and I found out that I had to add a MenuItem to the custom buttons region. And yes, I created a model too.

This is the code for adding the button in the module's main class.

[code]container.RegisterType<ISurveyButtonViewModel, SurveyButtonViewModel>();
                container.RegisterType<ISurveyButtonView, SurveyButton>();

                viewManager.ViewsByRegionName["BundleCustomButtonRegion"].Insert(0,
                  new ViewActivator()
                  {
                      ViewType = typeof(ISurveyButtonView),
                      ViewName = "SurveyButton",
                      ActivateView = true,
                      Condition = Conditions.ButtonViewToolBarCondition
                  }
                );[/code]

Where the condition is:
[code]public static class Conditions
    {
        public static bool ButtonViewToolBarCondition(ref object context)
        {
            var contextDictionary = (IDictionary<string, object>)context;

            var contextCase = (ICase)contextDictionary["Case"];

            return (contextCase != null) && (contextCase.MainInteraction is IInteractionEmail);
        }
    }[/code]

The model is similar to other view models, but here's the code for the view itself.

[code]<MenuItem x:Class="[NAMESPACE].SurveyButton"
            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:local="clr-namespace:[NAMESPACE]"
            mc:Ignorable="d" Height="43.204" Width="41.329" 
            Name="button" Click="Button_Click" ToolTip="Send survey"
            Header="button">
    <MenuItem.Icon>
        <Image Width="24" Height="24" Source="Survey.png"/>
    </MenuItem.Icon>
</MenuItem>[/code]