我已经制作了两个用户控件并将其包含在我的主用户控件中。之后我在我的一个表单中使用了该用户控件,但现在的问题是它向我显示了一个无法创建实例的错误。当我将鼠标悬停在主窗体中的usercontrol引用上时,它表示'值不能为空参数名称:g'。给出错误的用户控件在窗口的第40行,特别是<pc:SearchProduct x:Name="ProductFilterResult" Margin="0,0,0,0"/>
。
任何帮助将不胜感激
UserControl:
<UserControl x:Class="CPOSApplication.UserControls.Products.SearchProduct"
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:uc="clr-namespace:CPOSApplication.UserControls.Products"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="500" Width="Auto" Padding="5">
<Grid Focusable="True" x:Name="ProductGrid" Loaded="ProductGrid_Loaded">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="Auto" MinWidth="194" />
</Grid.ColumnDefinitions>
<Border Grid.ColumnSpan="2" Style="{DynamicResource SearchGridHeaderBorder}">
<DockPanel Grid.Row="0">
<Label x:Name="GridLabel" Style="{DynamicResource HeadingLabelsCustomStyle}" Content="Search Product:"/>
<Button x:Name="AddBtn" Style="{DynamicResource AddButtonCustomStyle}" Content="Add Product (F10)"/>
</DockPanel>
</Border>
<uc:ProductFilterResult x:Name="productFilterResult" Grid.Column="0" Grid.Row="1" HorizontalAlignment="Left" Margin="5,0"/>
<uc:FilterProduct x:Name="filterProduct" Grid.Column="1" Grid.Row="1" Width="300" Padding="2,0,0,0" Margin="5,0"/>
</Grid>
</UserControl>
UserControl.CS文件
using BusinessLogic;
using CPOSApplication.Classes.Managers;
using CPOSApplication.Forms;
using CPOSApplication.GeneralControl;
using Models.DatabaseModels;
using Models.HelpingModels;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace CPOSApplication.UserControls.Products
{
/// <summary>
/// Interaction logic for SearchProduct.xaml
/// </summary>
public partial class SearchProduct : UserControl
{
#region[Global Properties]
ProductSearchViewModel filters = new ProductSearchViewModel();
MainDashboard MainWindow;
SearchResultModel<ProductViewModel> GridList = new SearchResultModel<ProductViewModel>();
Guid CurrentApplicationId = new Guid(System.Configuration.ConfigurationManager.AppSettings["ApplicationId"]);
int RecordsPerPage = 16;
#endregion
#region[Contructor]
public SearchProduct()
{
InitializeComponent();
#region[Events Registration]
this.Loaded += new RoutedEventHandler(ProductView_Loaded);
this.Unloaded += new RoutedEventHandler(ProductView_UnLoaded);
productFilterResult.pager.PageSelected += pager_PageSelected;
productFilterResult.Edit += Edit_Click;
productFilterResult.Delete += Delete_Click;
AddBtn.Click += Add_Click;
filterProduct.FilterBtn.Click += FilterBtn_Click;
filterProduct.ClearBtn.Click += ClearBtn_Click;
#endregion
#region[Search Filters]
SetFilterParameters(null);
#endregion
GridList = new Logic(GetCurrentApplicationId()).GetProductsByFilter(filters);
RefreshResultGrid(GridList, true);
}
#endregion
#region[Events]
void ProductView_Loaded(object sender, RoutedEventArgs e)
{
MainWindow = (MainDashboard)Window.GetWindow(this);
#region[Hotkey Events Initialization]
HotKeyManager.RegisterMultiKeyGesture(HotActions.DeleteProductPopUp, new MultiKeyGesture(new Key[] { Key.D }, ModifierKeys.Control, "Delete"), DeleteHotKeyPressed);
HotKeyManager.RegisterMultiKeyGesture(HotActions.EditProduct, new MultiKeyGesture(new Key[] { Key.E }, ModifierKeys.Control, "Edit"), EditHotKeyPressed);
HotKeyManager.RegisterKey(HotActions.AddProduct, new KeyGesture(Key.F10, ModifierKeys.None), F10Pressed);
HotKeyManager.RegisterKey(HotActions.ClearProductFilters, new KeyGesture(Key.F9, ModifierKeys.None), F9Pressed);
HotKeyManager.RegisterKey(HotActions.SearchProductFilter, new KeyGesture(Key.F7, ModifierKeys.None), F7Pressed);
//HotKeyManager.RegisterKey(HotActions.AddProduct, new KeyGesture(Key.S, ModifierKeys.Shift | ModifierKeys.Control), CTRL);
#endregion
}
void ProductView_UnLoaded(object sender, RoutedEventArgs e)
{
List<HotActions> CurrentWindowActions = new List<HotActions>()
{
HotActions.AddProduct,
HotActions.DeleteProductPopUp,
HotActions.EditProduct,
HotActions.ClearProductFilters,
HotActions.SearchProductFilter
};
HotKeyManager.RemoveBindings(CurrentWindowActions);
}
void pager_PageSelected(GeneralControl.PaginationControl.Paginator sender, int total, int selectedPage)
{
filters.CurrentPage = selectedPage;
filters.CalculateTotal = false;
GridList = new Logic(GetCurrentApplicationId()).GetProductsByFilter(filters);
RefreshResultGrid(GridList, false);
}
private void FilterBtn_Click(object sender, RoutedEventArgs e)
{
FilterProducts();
}
private void ClearBtn_Click(object sender, RoutedEventArgs e)
{
ClearTextBoxes(this);
}
private void Delete_Click(object sender, RoutedEventArgs e)
{
ProductViewModel Product = (ProductViewModel)(sender as Image).Tag;
DeleteProductPopup(Product);
}
private void Edit_Click(object sender, RoutedEventArgs e)
{
ProductViewModel Product = (ProductViewModel)(sender as Image).Tag;
UpdateProductPopup(Product);
}
private void Add_Click(object sender, RoutedEventArgs e)
{
UpdateProductPopup(null);
}
private void ProductGrid_Loaded(object sender, RoutedEventArgs e)
{
ProductGrid.Focus();
}
#endregion
#region[HotKeys Events]
private void F10Pressed(HotKeyBindData sender)
{
UpdateProductPopup(null);
}
private void DeleteHotKeyPressed(HotKeyBindData sender)
{
string PressedKeys = MultiKeyGesture.GetPressedKeys();
if (!string.IsNullOrEmpty(PressedKeys))
{
PressedKeys = PressedKeys.Replace("D", "");
ProductViewModel BindedModel = null;
var dg = productFilterResult.ProductsGrid;
for (int i = 0; i < dg.Items.Count; i++)
{
DataGridRow row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(i);
var cellContent = dg.Columns[1].GetCellContent(row);
var cellContentPresenter = (ContentPresenter)cellContent;
DataTemplate editingTemplate = cellContentPresenter.ContentTemplate;
TextBlock CellTextBox = editingTemplate.FindName("IndexCell", cellContentPresenter) as TextBlock;
if (CellTextBox != null)
{
string CellValue = CellTextBox.Text;
if (!string.IsNullOrEmpty(CellValue))
{
if (CellValue == PressedKeys)
{
BindedModel = (ProductViewModel)cellContentPresenter.Content;
break;
}
}
}
}
if (BindedModel != null)
{
ProductViewModel model = BindedModel;
DeleteProductPopup(model);
}
}
}
private void EditHotKeyPressed(HotKeyBindData sender)
{
string PressedKeys = MultiKeyGesture.GetPressedKeys();
if (!string.IsNullOrEmpty(PressedKeys))
{
PressedKeys = PressedKeys.Replace("D", "");
ProductViewModel BindedModel = null;
var dg = productFilterResult.ProductsGrid;
for (int i = 0; i < dg.Items.Count; i++)
{
DataGridRow row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(i);
var cellContent = dg.Columns[1].GetCellContent(row);
var cellContentPresenter = (ContentPresenter)cellContent;
DataTemplate editingTemplate = cellContentPresenter.ContentTemplate;
TextBlock CellTextBox = editingTemplate.FindName("IndexCell", cellContentPresenter) as TextBlock;
if (CellTextBox != null)
{
string CellValue = CellTextBox.Text;
if (!string.IsNullOrEmpty(CellValue))
{
if (CellValue == PressedKeys)
{
BindedModel = (ProductViewModel)cellContentPresenter.Content;
break;
}
}
}
}
if (BindedModel != null)
{
ProductViewModel model = BindedModel;
UpdateProductPopup(model);
}
}
}
private void F9Pressed(HotKeyBindData sender)
{
ClearTextBoxes(this);
}
private void F7Pressed(HotKeyBindData sender)
{
FilterProducts();
}
#endregion
#region[Events Supporting Functions]
private void RefreshResultGrid(SearchResultModel<ProductViewModel> result, bool RefrestCounter)
{
productFilterResult.ProductsGrid.ItemsSource = result.ResultList;
if (RefrestCounter)
{
productFilterResult.pager.setCounts(result.TotalCount, filters.RecordsPerPage);
}
}
private void SetFilterParameters(string ProductName)
{
filters.Name = ProductName;
filters.CurrentPage = 1;
filters.RecordsPerPage = RecordsPerPage;
filters.CalculateTotal = true;
}
private void UpdateProductPopup(ProductViewModel Product)
{
try
{
MainWindow.modelPanelBG.Visibility = Visibility.Visible;
Window window = new Window();
window.Content = new UpdateProduct(window, MainWindow, Product);
window.WindowStyle = WindowStyle.None;
window.Width = 500;
window.Height = 200;
window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
window.AllowsTransparency = true;
window.ShowDialog();
}
catch (Exception ex)
{
}
}
private void DeleteProductPopup(ProductViewModel Product)
{
try
{
MainWindow.modelPanelBG.Visibility = Visibility.Visible;
Window window = new Window();
window.Content = new DeleteProduct(window, MainWindow, Product);
window.WindowStyle = WindowStyle.None;
window.Width = 300;
window.Height = 120;
window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
window.AllowsTransparency = true;
bool delete = window.ShowDialog() ?? false;
if (delete == true)
{
GridList.ResultList.Remove(Product);
}
}
catch (Exception ex)
{
}
}
void ClearTextBoxes(DependencyObject obj)
{
TextBox tb = obj as TextBox;
if (tb != null)
tb.Text = "";
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj as DependencyObject); i++)
ClearTextBoxes(VisualTreeHelper.GetChild(obj, i));
}
private void FilterProducts()
{
SetFilterParameters(filterProduct.ProductName.Text);
GridList = new Logic(GetCurrentApplicationId()).GetProductsByFilter(filters);
RefreshResultGrid(GridList, true);
}
private Guid GetCurrentApplicationId()
{
string ApplicationIdConfig = System.Configuration.ConfigurationManager.AppSettings["ApplicationId"];
if (string.IsNullOrEmpty(ApplicationIdConfig))
return new Guid();
else
return new Guid(ApplicationIdConfig);
}
#endregion
}
}
窗口
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:pc="clr-namespace:CPOSApplication.UserControls.Products"
xmlns:mpc="clr-namespace:CPOSApplication.UserControls.ModelPanelBackground"
xmlns:muc="clr-namespace:CPOSApplication.UserControls.Menus"
xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Name="mainWindow" x:Class="CPOSApplication.Forms.MainDashboard"
Title="CPOS" Height="720" Width="1024" WindowStartupLocation="CenterScreen" AllowsTransparency="True" WindowStyle="None" Icon="/CPOSApplication;component/Resources/logo.png">
<Grid x:Name="mainDashboardForm">
<mpc:ModelPanelBackground x:Name="modelPanelBG" Margin="0,0,0,10" Panel.ZIndex="1" Visibility="Collapsed"/>
<DockPanel LastChildFill="True">
<DockPanel x:Name="topHeaderDockPanel" Height="50" LastChildFill="False" DockPanel.Dock="Top" Background="#FFEEEEEE">
<DockPanel x:Name="topLogoDockPanel" Height="40" LastChildFill="False" VerticalAlignment="Top" Width="100" Margin="10,5,0,0" HorizontalAlignment="Left">
<Image x:Name="topLogoImage" Height="25" VerticalAlignment="Top" Width="95" Margin="0,7,0,5" Source="/CPOSApplication;component/Resources/logo.png"/>
</DockPanel>
<DockPanel x:Name="topRightCloseButtonDockPanel" Height="40" LastChildFill="False" VerticalAlignment="Top" Width="35" DockPanel.Dock="Right" Margin="0,5,10,0">
<Button x:Name="topRightCloseButton" Content="X" Style="{DynamicResource CloseBtn}" Click="topRightCloseButton_Click"/>
</DockPanel>
<DockPanel x:Name="topRightSettingDockPanel" Height="40" LastChildFill="False" Margin="0,5,5,0" VerticalAlignment="Top" Width="35" DockPanel.Dock="Right"/>
<DockPanel x:Name="topRightDropDownDockPanel" Height="40" LastChildFill="False" VerticalAlignment="Top" Width="220" DockPanel.Dock="Right" Margin="0,5,5,0"/>
<DockPanel x:Name="topHeaderMenuDockPanel" Height="40" LastChildFill="False" VerticalAlignment="Top" Width="240" DockPanel.Dock="Right" Margin="0,5,5,0">
<ToolBar x:Name="topHeaderMenuToolBar" Height="35" VerticalAlignment="Top" Margin="3,3,0,0" FontFamily="Arial" FontSize="16" Foreground="#FF9B9B9B" Background="{x:Null}" Width="232" Style="{DynamicResource TitleToolBarStyle}">
<Button x:Name="topHeaderFeedbackButton" Content="Feedback" Margin="5,0" Style="{DynamicResource topHeaderButtonCustomStyle}" Height="30" Padding="4" />
<Button x:Name="topHeaderHelpButton" Content="Help" Margin="5,0" Style="{DynamicResource topHeaderButtonCustomStyle}" Height="30" Padding="4" />
<Button x:Name="topHeaderUpdateButton" Content="Update" Margin="5,0" Style="{DynamicResource topHeaderButtonCustomStyle}" Height="30" Padding="4"/>
</ToolBar>
</DockPanel>
</DockPanel>
<DockPanel x:Name="topMainMenuHeader" Height="110" DockPanel.Dock="Top" LastChildFill="False" Background="#FFDAD9D9">
<muc:UserMenu x:Name="UserMenu" Margin="0"/>
</DockPanel>
<DockPanel x:Name="bottomStatusDockPanel" Height="28" DockPanel.Dock="Bottom" Background="#FFEEEEEE">
<Label x:Name="bottomStatusLabel" Content="Status:" Height="28" VerticalAlignment="Top" Width="57" HorizontalAlignment="Left" FontWeight="Bold" FontFamily="Arial" Foreground="#FFAEABAB" Padding="5,6,5,5"/>
<Label x:Name="bottomStatusContent" Content="" Height="28" VerticalAlignment="Top" Width="Auto" HorizontalAlignment="Left" FontWeight="Bold" FontFamily="Arial" Foreground="#FFAEABAB" Padding="5,6,5,5"/>
</DockPanel>
<DockPanel x:Name="middleContentAreaDockPanel" Margin="0" DockPanel.Dock="Top" Width="Auto">
<Grid>
<pc:SearchProduct x:Name="ProductFilterResult" Margin="0,0,0,0"/>
</Grid>
</DockPanel>
</DockPanel>
</Grid>
</Window>
答案 0 :(得分:3)
我在这个答案上迟了几年,但希望这可以节省一些人追踪它的时间。
您的代码使用ConfigurationManager.AppSettings中的值调用Guid构造函数。如果找不到,则可以返回空值,尤其是在设计时发生。
检查以下两行,并确保ConfigurationManager提供非空字符串。
return new Guid(ApplicationIdConfig);
Guid CurrentApplicationId = new Guid(System.Configuration.ConfigurationManager.AppSettings["ApplicationId"]);
答案 1 :(得分:0)
正如Matt所说,当您将空字符串或字节数组作为输入时,问题出在Guid构造函数上。 这里有一个例子: