在NerdDinners示例之后,我有兴趣创建一个强类型的母版页.为了实现这一点,我使用一个基本控制器来检索母版页的数据.所有其他控制器都继承此类.同样,我有ViewModels主页和任何其他视图.视图ViewModel类继承自母版页ViewModel.
子控制器应该如何确保将母版页的数据传递给View而不设置ViewModel与母版页本身相关的属性?
我的母版页将显示一些按钮,这些按钮在XML文件中确定,因此Buttons我填充的类.
MasterPage ViewModel代码段
using System.Collections.Generic;
namespace Site1.Models
{
public class MasterViewModel
{
public List<Button> Buttons{set; get;}
}
}
Run Code Online (Sandbox Code Playgroud)
视图 ViewModel
namespace Site1.Models
{
public class View1ViewModel : MasterViewModel
{
public SomeDataClass SomeData { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
using System.Collections.Generic;
using System.Web.Mvc;
using Site1.Models;
namespace Site1.Controllers
{
public abstract class BaseController : Controller
{
protected MasterViewModel model = new MasterViewModel();
public BaseController()
{
model.Buttons …Run Code Online (Sandbox Code Playgroud) 我发现无法使用with-param来处理apply-templates.举个例子,我已经破解了w3schools中给出的例子.
XSL
<xsl:template match="/">
<xsl:apply-templates>
<xsl:with-param name="test" select="'has this parameter been passed?'"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="cd">
<xsl:param name="test"></xsl:param>
parameter:
<xsl:value-of select="$test"></xsl:value-of>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)
XML
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
</catalog>
Run Code Online (Sandbox Code Playgroud)
(希望)你会看到测试参数没有传递给cd模板.我可以在使用call-template时使用它,但不能使用apply-templates.这是怎么回事?我正在使用XSL 1.0.请忽略我传递硬编码参数的事实 - 这只是一个例子.
我似乎在这段代码中有内存泄漏.它是一个控制台应用程序,它创建了几个类(WorkerThread),每个类都以指定的时间间隔写入控制台.所述Threading.Timer用于做到这一点,因此写入到控制台在一个单独的线程执行(该TimerCallback在从线程池截取的单独的线程调用).更复杂的是,MainThread类挂接到FileSystemWatcher的Changed事件; 当test.xml文件更改时,将重新创建WorkerThread类.
每一次,该文件被保存(每次所述的WorkerThread,因此定时器重新创建),在任务管理器增加存储器(内存使用,有时也VM大小); 此外,在.net内存分析器(V3.1),通过两个的WorkerThread类增加未予处置实例(这可能是一个红色的鲱鱼,但因为我读过的.Net内存分析器有一个bug由此挣扎检测处理班级.
无论如何,这是代码 - 有谁知道什么是错的?
编辑:我已经将类创建移出FileSystemWatcher.Changed事件处理程序,这意味着WorkerThread类总是在同一个线程中创建.我为静态变量添加了一些保护.我还提供了线程信息,以更清楚地显示正在发生的事情,并使用Timer与显式线程交换; 但是,内存仍然在泄漏!内存使用量一直在缓慢增加(这只是由于控制台窗口中的额外文本?),并且当我更改文件时VM大小增加.这是代码的最新版本:
编辑当你写入它时,这似乎主要是控制台使用内存的问题.显式编写的Threads仍然存在增加内存使用量的问题.请参阅下面的答案.
class Program
{
private static List<WorkerThread> threads = new List<WorkerThread>();
static void Main(string[] args)
{
MainThread.Start();
}
}
public class MainThread
{
private static int _eventsRaised = 0;
private static int _eventsRespondedTo = 0;
private static bool _reload = false;
private static readonly object _reloadLock = new object();
//to do something once in handler, though
//this code would go in onStart in a windows service.
public …Run Code Online (Sandbox Code Playgroud) 我正在使用Graphics.DrawImage方法调整jpeg的大小(参见下面的代码片段).任何人都可以确认这不会影响新图像的压缩吗?我见过这个帖子,但我特别谈到jpegs的压缩.
private byte[] getResizedImage(String url, int newWidth)
{
Bitmap bmpOut = null;
System.IO.MemoryStream outStream = new System.IO.MemoryStream();
//input image is disposable
using (Bitmap inputImage = LoadImageFromURL(url))
{
ImageFormat format = inputImage.RawFormat;
decimal ratio; //ratio old width:new width
int newHeight = 0;
//*** If the image is smaller than a thumbnail just return it
if (inputImage.Width < newWidth)
return null;
ratio = (decimal)newWidth / inputImage.Width;
decimal h = inputImage.Height * ratio;
newHeight = (int)h;
bmpOut = new Bitmap(newWidth, newHeight); …Run Code Online (Sandbox Code Playgroud) 我希望能够使用CamelCasePropertyNameContractResolver但覆盖特定的属性名称.为此,我使用该JsonProperty属性.这种方法很好,除非我选择的名称是完全大写的.任何想法有什么问题或如何解决它?
在下面的示例中,Bar序列化为"BAR"当我不使用CamelCasePropertyNameContractResolver时,但"bar"在我使用解析器时被序列化.Foo并CamelCaseProperty在两种情况下都能正确序列化.
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace ConsoleTester
{
class Program
{
static void Main(string[] args)
{
var foo = new FooBar {CamelCaseProperty = "test", Foo = "test", Bar = "test" };
var output = JsonConvert.SerializeObject(foo);
// output "CamelCaseProperty", "fOO", "BAR"
var output2 = JsonConvert.SerializeObject(foo, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() });
// output "camelCaseProperty", "fOO", "bar"
}
}
public class FooBar
{
public string …Run Code Online (Sandbox Code Playgroud) .net ×3
c# ×3
asp.net-mvc ×1
compression ×1
controller ×1
image ×1
json.net ×1
master-pages ×1
memory ×1
parameters ×1
resize ×1
timer ×1
viewmodel ×1
xslt ×1