我正在使用带有Model-View-ViewModel模式的WPF.因此,我的代码隐藏文件(.xaml.cs)都是空的,除了调用InitializeComponent的构造函数.因此,对于每个.xaml文件,我都有一个匹配的,无用的.xaml.cs文件.
我发誓我读到某个地方,如果文件后面的代码是空的,除了构造函数,有一种方法可以从项目中删除该文件.在搜索网络后,似乎适当的方法是使用'x:Subclass'属性:
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit"
x:Class="MyNamespace.MyClass"
x:Subclass="UserControl"
d:DesignWidth="700" d:DesignHeight="500">
Run Code Online (Sandbox Code Playgroud)
这将在生成的.g.cs文件中执行以下操作:
似乎很完美.实际上,如果你在构建中仍然有.xaml.cs文件,它会因为缺少部分而不再编译 - 所以我认为这必须是正确的.但是,如果我从构建中删除多余的文件并运行,则控件不会正确初始化(它是空白的).我认为这是因为没有调用InitializeComponent().我看到InitializeComponent不是虚拟的,所以基类似乎没有办法调用它(没有使用反射).
我错过了什么吗?
谢谢!
埃里克
我想CsharpFunction从JavaScript中调用代码隐藏中的C#函数.我想下面的代码,但JavaScript的条件是True或False,CsharpFunction被称为无关!
JavaScript代码:
if (Javascriptcondition > 0) {
<%CsharpFunction();%>
}
Run Code Online (Sandbox Code Playgroud)
C#代码背后:
protected void CsharpFunction()
{
// Notification.show();
}
Run Code Online (Sandbox Code Playgroud)
如何从JavaScript调用C#函数?
我想实现一个支付服务.我将在后面的代码中创建一些值,然后使用post方法我必须将此值发布到支付网关,用户必须重定向到该页面.
我不能使用表单操作,因为我必须创建一些值并在代码后面的db中保存一些东西.
我该怎么实现呢?如果我可以将数据发布到我的应用程序上的另一个页面,并且可以通过程序提交该页面,那么它可能对我有用
谢谢
我在"App_Code"文件夹中的文件中有一个类.我可以在"aspx"文件中使用它,但不能从代码隐藏文件中使用它.如何让代码隐藏可见?
注意:这是Mono上的ASP.Net,我直接编写类,而不是使用IDE来编译它们
我的文件:
<%@ Page language="c#" src="TestAppCode.aspx.cs" Inherits="TestAppCode.TestAppCode" AutoEventWireup="true" %>
<html>
<head>
<title>Test App_Code Folder</title>
</head>
<body>
<form id="contactForm" runat="server">
<asp:TextBox id="Name" runat="server" ></asp:TextBox>
<asp:TextBox id="Age" runat="server" ></asp:TextBox>
<asp:Button ID="Submit" runat="server" Text="Submit" onclick="SubmitForm" />
</form>
</body>
<html>
Run Code Online (Sandbox Code Playgroud)
using System;
using System.Web.UI.WebControls;
namespace TestAppCode
{
public class TestAppCode : System.Web.UI.Page
{
protected void SubmitForm(object sender, EventArgs e)
{
//It fails here with the error: CS0246: The type or namespace name
//`MyAppCodeClass' could not be found. Are you missing …Run Code Online (Sandbox Code Playgroud) 首先,我改变HyperLink.NavigateUrl了代码隐藏Page_Load().
但在我决定使用Eval()方法设计之后.
<asp:HyperLink runat="server"
NavigateUrl='<%# String.Format("~/Refuse.aspx?type={0}&id={1}", Eval("type"), Eval("id")) %>' Text="Refuse" />
Run Code Online (Sandbox Code Playgroud)
要么
<asp:HyperLink ID="urlRefuse" runat="server"
NavigateUrl='<%# String.Format("~/Refuse.aspx?type={0}&id={1}", Request["type"], Request["id"]) %>' Text="Refuse" />
Run Code Online (Sandbox Code Playgroud)
where id和type- 是来自的变量Request.
但它不起作用.仅显示原始文本"拒绝".我的错误在哪里?提前致谢.
如何以编程方式向datatemplates添加控件?
例如.下面我创建了TextBlock和DataTemplate.
TextBlock text = new TextBlock();
DataTemplate template = new DataTemplate();
Run Code Online (Sandbox Code Playgroud)
现在我需要将TextBlock添加到DataTemplate.怎么做到这一点?
我知道后面的代码中还有其他addind数据模板的方法1.在XAML中创建数据模板并将其加载到后面的代码上2.使用XamlParser创建和添加
但我需要按照我在示例中展示的方式来做.
需要一些帮助.
ASP.NET,C#
正如标题所示,我想知道是否有人知道如何以编程方式(文件后面的c#代码)将div添加到另一个容器div(在aspx页面中).
提前致谢
我试图将一个非常简单的属性绑定到TextBlock,但我必须在代码隐藏(C#)中完成所有操作.
我想做的是:
public string SomeText { get; set; }
Run Code Online (Sandbox Code Playgroud)
在我尝试TextBlock上的绑定之后:
Binding myBinding = new Binding(SomeText);
myTextBlock.SetBinding(TextBlock.TextProperty, myBinding);
Run Code Online (Sandbox Code Playgroud)
如何使TextBlock的Text属性与Property保持一致SomeText.
我有一个名为'MyTemplate.xaml'的XAML文件中定义的模板.此模板使用名为"MyTemplate.cs"的代码隐藏文件.
Mytemplate.xaml:
<ResourceDictionary x:Class="Project.Templates.MyTemplate">
<DataTemplate ... />
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
MyTemplate.cs:
namespace Project.Templates
{
public partial class MyTemplate : ResourceDictionary
{
...
}
}
Run Code Online (Sandbox Code Playgroud)
在Visual Studio解决方案资源管理器中,这两个文件是并排的.我想要做的是将这两个文件放在一起,就像使用Control及其代码隐藏一样.
是)我有的: 
我想拥有什么: 
最好的方法是什么?谢谢.
code-behind ×10
c# ×5
asp.net ×4
wpf ×4
.net ×2
xaml ×2
app-code ×1
binding ×1
datatemplate ×1
dynamic ×1
eval ×1
javascript ×1
mono ×1
mvvm ×1
treeview ×1