Beispiel #1
0
        private List <CommandBarButtonItem> CheckForToolbarItems(object dataContext)
        {
            var buttonList = new List <CommandBarButtonItem>();
            var members    = GetMembers(dataContext);

            foreach (var member in members)
            {
                var toolbarButtonAttribute = member.GetCustomAttribute <ToolbarButtonAttribute>(false);
                var captionAttribute       = member.GetCustomAttribute <CaptionAttribute>();
                var caption = captionAttribute != null ? captionAttribute.Caption : string.Empty;

                if (toolbarButtonAttribute != null)
                {
                    var title = caption ?? toolbarButtonAttribute.Title;

                    ICommand command    = null;
                    var      methodInfo = member as MethodInfo;

                    if (methodInfo != null)
                    {
                        command = new ReflectiveCommand(dataContext, member as MethodInfo, null);
                    }

                    CommandBarButtonItem button = null;

                    if (!string.IsNullOrEmpty(title))
                    {
                        button = new CommandBarButtonItem(title, toolbarButtonAttribute.Style, delegate { command.Execute(null); });
                    }
                    else
                    {
                        button       = new CommandBarButtonItem(toolbarButtonAttribute.ButtonType, delegate { command.Execute(null); });
                        button.Style = toolbarButtonAttribute.Style;
                    }

                    button.Enabled  = true;
                    button.Location = toolbarButtonAttribute.Location;

                    var orderAttribute = member.GetCustomAttribute <OrderAttribute>();
                    if (orderAttribute != null)
                    {
                        button.Order = orderAttribute.Order;
                    }
                    else
                    {
                        button.Order = 0;
                    }

                    buttonList.Add(button);
                }
            }

            if (buttonList.Count > 0)
            {
                var sortedList = buttonList.OrderBy(button => button.Tag).ToList();
                return(sortedList);
            }

            return(null);
        }
        public GamePackageLoaderPickerCP()
        {
            MethodInfo method = this.GetPrivateMethod(nameof(ProcessItemAsync));

            ItemSelectedCommand = new ReflectiveCommand(this, method, canExecuteM: null !);
            Visible             = true; //for this one, show as true.
        }
        private static void BindCommands(object viewModel, object view, IEnumerable <MethodInfo> methods, List <PropertyInfo> properties)
        {
            foreach (var method in methods)
            {
                if (method.IsSpecialName ||
                    method.ReturnType != typeof(void) ||
                    method.GetParameters().Length > 0 ||
                    !method.IsPublic)
                {
                    continue;
                }

                var foundControl = GetControl(view, method.Name);
                if (foundControl == null)
                {
                    if (debug)
                    {
                        log.Debug("No button found for " + viewModel.GetType().Name + "." + method.Name + "()");
                    }
                    continue;
                }

                var canMethodName = "Can" + method.Name;
                var foundProperty = properties.FirstOrDefault(x => x.Name == canMethodName);
                if (foundProperty == null)
                {
                    if (debug)
                    {
                        log.Debug(canMethodName + "() was not found on class " + viewModel.GetType().Name);
                    }
                }
                else
                {
                    properties.Remove(foundProperty);
                }

                var command = new ReflectiveCommand(viewModel, method, foundProperty);
                if (TrySetCommand(foundControl, command))
                {
                    if (debug)
                    {
                        log.Debug(view.GetType().Name + "." + foundControl.Name + " => " + viewModel.GetType().Name + "." + method.Name);
                    }
                }
                else
                {
                    log.Error("Failed binding " + view.GetType().Name + "." + foundControl.Name + " to " + viewModel.GetType().Name + "." + foundProperty.Name);
                }
            }
        }
Beispiel #4
0
        private static ICommand GetCommand(object viewModel, MethodInfo method, MethodInfo?validateM, PropertyInfo?foundProperty)
        {
            var item = method.GetCustomAttribute <CommandAttribute>();

            if (item == null)
            {
                throw new BasicBlankException("Was not even a custom command.  Rethink");
            }
            ICommand?output;

            if (!(viewModel is IBlankGameVM blank))
            {
                throw new BasicBlankException("This is not a blank game view model.  Rethink");
            }
            if (blank.CommandContainer == null)
            {
                throw new BasicBlankException("The command container for command not there.  Rethink");
            }
            switch (item.Category)
            {
            case EnumCommandCategory.Plain:
                if (foundProperty == null && validateM != null)
                {
                    output = new PlainCommand(viewModel, method, validateM, blank.CommandContainer);
                }
                else
                {
                    output = new PlainCommand(viewModel, method, foundProperty !, blank.CommandContainer);
                }
                break;

            case EnumCommandCategory.Game:
                if (!(viewModel is IBasicEnableProcess basics))
                {
                    throw new BasicBlankException("You need to implement the IEnableAlways in order to use out of turn command.  Rethink");
                }
                if (foundProperty == null && validateM != null)
                {
                    output = new BasicGameCommand(basics, method, validateM, blank.CommandContainer);
                }
                else
                {
                    output = new BasicGameCommand(basics, method, foundProperty !, blank.CommandContainer);
                }
                break;

            case EnumCommandCategory.Limited:
                if (!(viewModel is IBasicEnableProcess basics2))
                {
                    throw new BasicBlankException("You need to implement the IEnableAlways in order to use out of turn command.  Rethink");
                }
                if (foundProperty == null && validateM != null)
                {
                    output = new LimitedGameCommand(basics2, method, validateM, blank.CommandContainer);
                }
                else
                {
                    output = new LimitedGameCommand(basics2, method, foundProperty !, blank.CommandContainer);
                }
                break;

            case EnumCommandCategory.OutOfTurn:

                if (!(viewModel is IEnableAlways enables))
                {
                    throw new BasicBlankException("You need to implement the IEnableAlways in order to use out of turn command.  Rethink");
                }

                output = new OutOfTurnCommand(enables, method, blank.CommandContainer);
                break;

            case EnumCommandCategory.Open:
                if (foundProperty == null && validateM != null)
                {
                    output = new OpenCommand(viewModel, method, validateM, blank.CommandContainer);
                }
                else
                {
                    output = new OpenCommand(viewModel, method, foundProperty !, blank.CommandContainer);
                }
                break;

            case EnumCommandCategory.Control:
                if (!(viewModel is IControlObservable control))
                {
                    throw new BasicBlankException("You need to implement the IControlVM in order to use control command.  Rethink");
                }
                output = new ControlCommand(control, method, blank.CommandContainer);
                break;

            case EnumCommandCategory.Old:
                if (foundProperty == null && validateM != null)
                {
                    output = new ReflectiveCommand(viewModel, method, validateM);
                }
                else
                {
                    output = new ReflectiveCommand(viewModel, method, foundProperty !);
                }
                break;

            default:
                throw new BasicBlankException("Not supported");
            }
            if (output == null)
            {
                throw new BasicBlankException("No command.   Rethink");
            }
            return(output);
        }